有没有办法可以将以下两个查询组合起来,通过一个查询返回两个数字。
var maxIDOne = mydb.GetMyData()
.Where(x => x.MyTypeID == 1)
.Max(x => x.ID);
var maxIDTwo = mydb.GetMyData()
.Where(x => x.MyTypeID == 2)
.Max(x => x.ID);
答案 0 :(得分:0)
var mydb = new[]{
new MyType{ MyTypeID = 1, ID = 2},
new MyType{ MyTypeID = 1, ID = 3},
new MyType{ MyTypeID = 2, ID = 5},
new MyType{ MyTypeID = 2, ID = 4},
};
var vals = from mt in mydb
where mt.MyTypeID == 1 || mt.MyTypeID == 2
group mt by mt.MyTypeID into g
select new { MyTypeId = g.Key, MaxID = g.Max(x => x.ID)};
返回
答案 1 :(得分:0)
根据您想要的结果,这样的事情可能有效:
var maxIdOneAndTwo = mydb.GetMyData()
.Where(x => x.MyTypeID == 1 || x.MyTypeID == 2)
.GroupBy(d => d.MyTypeID)
.Select(g => new {
MyTypeID = g.Key,
MaxID = g.Max(x.ID)
});
为了记录,我没有提及任何外部资源来撰写这个答案。