简体   繁体   中英

Linq Query with a where condition - count items

How can I change this request :

query = query.Where(item => (from table in context.Table 
                             where table.amount == item.Amount 
                             select table).Count() >= 10);

to not use the subquery (from ... in ...) ?

I tried to create the subquery separately, to use it with the Where condition :

var subQuery = from table in context.Table select table.amount;

var list = subQuery.ToList()

But I don't know how I can use it after that, because of the .Count() operation.

Thank you for your comments.

How about this:

query = query.Where(item => context.Table
                                   .Count(t => t.amount == item.Amount) >= 10);

Or to reduce the number of round-trips:

var counts = context.Table
                    .GroupBy(t => t.amount)
                    .Select(g => new {amount = g.Key, count = g.Count()});

query = from q in query
        join c in counts
        on q.amount equals c.amount
        where c.count >= 10
        select q;

只需将Count与谓词直接一起使用:

query.Where(item => context.Table.Count(table.amount == item.Amount) >= 10);

what about this one

var subQuery = (from table in context.Table select table.amount).ToList();

query = query.Where(item => subQuery.Count() >= 10);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM