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
我想在此结果集中添加最大和最小日期列。我怎样才能做到这一点。 如果两个数字之间只有一个呼叫,则最大和最小日期相同
答案 0 :(得分: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) });
会给你
答案 1 :(得分: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()))
});