我昨天开始使用EF,我很难将这个简单的查询转换为EF sintax
翻译:
select a.city from offer o, address a, offer_address oa
where o.identifier = oa.offeridentifier
and a.identifier = oa.addressidentifier
group by a.city
order by count(*) desc
分为:
var cities = (from o in db.offer
from a in db.address
from oa in db.offer_address
where (o.identifier == oa.offeridentifier
&& a.identifier == oa.addressidentifier)
group a by a.city into c
select new
{
quantity = c.Count(),
city = c.Key
}).OrderByDescending(a => a.quantity).Select(a => a.city);
var cityCollection = new List<string>();
foreach (var city in cities)
cityCollection.Add(city.ToString());
我试过了你的成功
var cities = (from oa in db.offer_address
from of in db.offer.Where(x => x.identifier == oa.identifier)
from ad in db.address.Where(y => y.identifier == oa.offeridentifier).AsEnumerable()
group ad.city by new { ad.city } into g
select new
{
quantity = g.Count(),
city = g.Key
}).OrderByDescending(a => a.quantity);
尝试进入第一个循环时会出现问题!
Unknown column 'GroupBy1.K1' in 'field list'`
Line 55: foreach (var city in cities)`
第二种情况:
Can't group on 'A1'
更新
此代码有效,但我不需要
var cities = (
from of in db.offer
from ad in db.address
from oa in db.offer_address
where (of.identifier == oa.offeridentifier && ad.identifier == oa.addressidentifier)
group ad.city by new { ad.city } into g
select new
{
quantity = g.Count()
}).OrderByDescending(a => a.quantity)
或者
var cities = (
from of in db.offer
from ad in db.address
from oa in db.offer_address
where (of.identifier == oa.offeridentifier && ad.identifier == oa.addressidentifier)
group ad.city by new { ad.city } into g
select new
{
city = g.Key
});
答案 0 :(得分:6)
更新 - 尝试在第一次选择
后添加AsEnumerable()
var query = (from o in db.offer
from a in db.address
from oa in db.offer_address
where (o.identifier == oa.offeridentifier && a.identifier == oa.addressidentifier)
group a by a.city into c
select new
{
quantity = c.Count(),
city = c.Key
})
.AsEnumerable()
.OrderByDescending(a => a.quantity)
.Select(a => a.city);