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() });

このクエリは私にこのような結果を与えます

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

この結果セットにも最大と最小の日付列を追加したいと思います。これどうやってするの。2つの番号の間に1つの呼び出ししかない場合、最大日と最小日は同じになります

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 に答える