1
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("dataTable");
dataTable.Columns.Add("caller", typeof(String));
dataTable.Columns.Add("callee", typeof(String));
dataTable.Rows.Add("999", "888",2012-11-06 12:11:30);
dataTable.Rows.Add("888", "999",2012-05-06 12:06:30);
dataTable.Rows.Add("888", "999",2010-05-06 06:06:30);
dataTable.Rows.Add("999", "555",2012-04-06 12:11:30);
dataTable.Rows.Add("555", "333",2012-01-06 12:11:30);
dataTable.Rows.Add("555", "999",2012-10-06 12:11:30);
dataSet.Tables.Add(dataTable);

var result = dataSet.Tables["dataTable"].Select().Select(dr =>
new
{
    caller1 = StringComparer.CurrentCultureIgnoreCase.Compare(dr["caller"], dr["callee"]) < 0 ? dr["callee"] : dr["caller"],
    caller2 = StringComparer.CurrentCultureIgnoreCase.Compare(dr["caller"], dr["callee"]) < 0 ? dr["caller"] : dr["callee"]
})

    .GroupBy(drg => new { drg.caller1, drg.caller2 } )
    .Select(drg => new { drg.Key.caller1, drg.Key.caller2, count = drg.Count() });

This query gives me result like this

caller1    caller2  count 
999        888      3
999        555      2
555        333      1

I wanna add max and min date column also in this result set. how can i do this. If there is only one call between two numbers max and min date will be same

4

2 回答 2

2

尝试这个:

DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("dataTable");
dataTable.Columns.Add("caller", typeof(String));
dataTable.Columns.Add("callee", typeof(String));
dataTable.Columns.Add("timestamp", typeof(String));
dataTable.Rows.Add("999", "888","2012-11-06 12:11:30");
dataTable.Rows.Add("888", "999","2012-05-06 12:06:30");
dataTable.Rows.Add("888", "999","2010-05-06 06:06:30");
dataTable.Rows.Add("999", "555","2012-04-06 12:11:30");
dataTable.Rows.Add("555", "333","2012-01-06 12:11:30");
dataTable.Rows.Add("555", "999","2012-10-06 12:11:30");
dataSet.Tables.Add(dataTable);

var result = dataSet.Tables["dataTable"].Select().Select(dr =>
    new {
        caller1 = StringComparer.CurrentCultureIgnoreCase.Compare(dr["caller"], dr["callee"]) < 0 ? dr["callee"] : dr["caller"],
        caller2 = StringComparer.CurrentCultureIgnoreCase.Compare(dr["caller"], dr["callee"]) < 0 ? dr["caller"] : dr["callee"],
        timestamp = DateTime.Parse(dr["timestamp"].ToString())
    })
    .GroupBy(drg => new { drg.caller1, drg.caller2 } )
    .Select(drg => new { drg.Key.caller1, drg.Key.caller2, count = drg.Count(), max = drg.Max(x=>x.timestamp), min = drg.Min(x=>x.timestamp) });

这会给你

在此处输入图像描述

于 2012-11-08T08:34:47.650 回答
0
DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("dataTable");
dataTable.Columns.Add("caller", typeof(String));
dataTable.Columns.Add("callee", typeof(String));
dataTable.Columns.Add("date", typeof(DateTime));
dataTable.Rows.Add("999", "888", new DateTime(2012, 11, 06, 12, 11, 30));
dataTable.Rows.Add("888", "999", new DateTime(2012, 05, 06, 12, 06, 30));
dataTable.Rows.Add("888", "999", new DateTime(2010, 05, 06, 06, 06, 30));
dataTable.Rows.Add("999", "555", new DateTime(2012, 04, 06, 12, 11, 30));
dataTable.Rows.Add("555", "333", new DateTime(2012, 01, 06, 12, 11, 30));
dataTable.Rows.Add("555", "999", new DateTime(2012, 10, 06, 12, 11, 30));
dataSet.Tables.Add(dataTable);

var result =
    dataSet.Tables["dataTable"]
        .Select()
        .GroupBy(item =>
            new 
            { 
                caller1 = Int32.Parse(item["caller"].ToString()) > Int32.Parse(item["callee"].ToString()) ? item["caller"] : item["callee"],
                caller2 = Int32.Parse(item["caller"].ToString()) > Int32.Parse(item["callee"].ToString()) ? item["callee"] : item["caller"]
            })

        .Select(item => 
            new
            {
                caller1 = item.Key.caller1,
                caller2 = item.Key.caller2,
                count = item.Count(),
                minDate = item.Min(min => DateTime.Parse(min["date"].ToString())),
                maxDate = item.Max(max => DateTime.Parse(max["date"].ToString()))
            });
于 2012-11-08T08:29:43.700 回答