LINQ-can';t创建请求
本文关键字:创建 请求 LINQ-can | 更新日期: 2023-09-27 17:59:16
你好,我有以下结构:
Table Countries:
- CountryID
- CountryName
Table Records:
- RecordID
- RecordName
- CountryID
此LINQ返回所有国家的列表"原样":
(from i in db.Countries
select new SelectListItem()
{
Text = i.CountryName,
Value = i.CountryID.ToString()
}).ToList();
我想把这个国家列表按这样的方式排序:在"记录"表中最受欢迎的国家排在最前面。怎么做?
您可以尝试类似的
(from i in db.Countries
join r in db.Records on i.CountryID equals r.CountryID into rds
orderby rds.Count() descending
select new SelectListItem()
{
Text = i.CountryName,
Value = i.CountryID.ToString()
}).ToList();
var recordsItem=db.Records.GroupBy(x=> x.CountryID).Select( gr => new{ CID=gr.Key, Count=gr.Count()}).ToList();
var result= (from c in db.Countries
join r in recordsItem on c.CountryID equals r.CID
order by r.Count descending
select new SelectListItem()
{
Text = c.CountryName,
Value = c.CountryID.ToString()
}).ToList();