开发者

Unknown column error using Entity Framework and LINQ

开发者 https://www.devze.com 2023-03-23 10:26 出处:网络
I started with EF yesterday and I am in trouble to transform this simple query into EF sintax Translate:

I started with EF yesterday and I am in trouble to transform this simple query into EF sintax

Translate:

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

Into:

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 = ne开发者_如何学Cw List<string>();
foreach (var city in cities)
    cityCollection.Add(city.ToString());

I have tried withou success

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);

The problem occurs when try to get inside the first loop!

Unknown column 'GroupBy1.K1' in 'field list'`

Line 55: foreach (var city in cities)`

With the second case:

Can't group on 'A1'

UPDATE

This code works, but its not i need

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)

Or

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
                });


Update - Try adding AsEnumerable() after the first select

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消