1
我想使用下面的語句分組一些數據庫數據。Groupby然後使用lambda/linq選擇數據
var res = from messages in db.Messages
group messages by messages.Message_Sent_Date into grp
select new { message_count = grp.Sum(g => g.Message_Count), message_cost = grp.Sum(g => g.Customer_Price) };
這相當於:
var query = db.Messages.Where(m => m.Message_Sent_Date != null && m.Message_Count != 99)
.GroupBy(d => d.Message_Sent_Date)
.Select(g => new {
message_count = g.Sum(c => c.Message_Count),
message_cost = g.Sum(c => c.Customer_Price),
});
我的問題是,所選擇的對象有2個字段即MESSAGE_COUNT和message_cost。如果我想添加第三個字段'Message_Sent_Date',它基本上是數據分組到的列。我怎麼做?
真棒。這工作! – psyoptica
很高興聽到和..高興地幫助你...! –