简体   繁体   中英

Set Difference of IEnumerable from IQueryable in Linq to Entities

I have two lists, one is IQueryable and the other is IEnumerable.

I need to find the set difference between them in Linq to Entities like,

IQueryable - IEnumerable, where T is a complex type (class).

When I tried using IQueryable.Except(IEnumerable), it did not work as expected.

var listQuery = select query; (listQuery becomes an IQueryable type)
var listEnumerable = (select query).ToList(); (listEnumerable becomes an IEnumerable type)
listQuery.Except(listEnumerable); 

(This does not perform the expected operation like, imagine, listQuery.Count() is 110 and listEnumerable.Count() is 30, so the final Except() should return 80, instead it returns the whole 110).

Any help?

Thanks Manikandan J

IQuerable将linq转换为sql语句并将其作为服务器触发,而IEnumerable在内存中设置了结果,并且它在内存中而不是服务器端执行所有操作。

If "Except" is not working (due to custom class type) then you need to implement IEqualityComparer on your custom class. There's a really good example here:

http://msdn.microsoft.com/en-us/library/vstudio/bb336390%28v=vs.100%29.aspx

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