Join between in memory collection and EntityFramework

前端 未结 4 1314
闹比i
闹比i 2020-12-30 04:11

Is there any mechanism for doing a JOIN between an in-memory collection and entity framework while preserving the order.

What I am trying is

var ite         


        
相关标签:
4条回答
  • 2020-12-30 04:20

    try this:

    var list = (from n in efRepo
               where myInMemoryList.Select(m=>m.RECORD_NUMBER).Contains(n.RECORD_NUMBER)
               select n).ToList();
    

    Contains will be translated to IN operator in SQL (only if your RECORD_NUMBER member is a primitive type like int, string, Guid, etc)

    0 讨论(0)
  • 2020-12-30 04:21

    You can read how you can do this with the PredicateBuilder from the LINQKit or Stored Procedures in my blog post.

    http://kalcik.net/2014/01/05/joining-data-in-memory-with-data-in-database-table/

    0 讨论(0)
  • 2020-12-30 04:25

    No you cannot join in-memory collection with database result set without loading whole result set to the memory and performing the join with linq-to-objects. Try using contains instead of join:

    var myNumbers = myInMemoryList.Select(i => i.RECORD_NUMBER);
    var itemsToAdd = efRepo.Where(e => myNumbers.Contains(e.RECORD_NUMBER));
    

    This will generate query with IN operator

    0 讨论(0)
  • 2020-12-30 04:31

    What about loading the whole efRepo? I mean something like this (ToArray()):

    var itemsToAdd = myInMemoryList.Join(
        efRepo.ToArray(),
        listitem => listitem.RECORD_NUMBER, efRepoItem => efRepoItem.RECORD_NUMBER, (left, right) => right);
    
    0 讨论(0)
提交回复
热议问题