簡體   English   中英

使用實體框架將表連接到列表

[英]Joining table to a list using Entity Framework

我有以下的Entity Framework函數,它將表連接到列表。 serviceSuburbList中的每個項目都包含兩個整數,即ServiceIdSuburbId

public List<SearchResults> GetSearchResultsList(List<ServiceSuburbPair> serviceSuburbList)
{
    var srtList = new List<SearchResults>();
    srtList = DataContext.Set<SearchResults>()
                         .AsEnumerable()
                         .Where(x => serviceSuburbList.Any(m => m.ServiceId == x.ServiceId && 
                                                                m.SuburbId == x.SuburbId))
                         .ToList();

    return srtList;
}

顯然, AsEnumerable了我的表現。 我不確定執行此操作的另一種方法。 基本上,我有SearchResults表,並且想查找與serviceSuburbList匹配的記錄。

如果serviceSuburbList的長度不大,則可以使幾個Union

var table = DataContext.Set<SearchResults>();
IQuerable<SearchResults> query = null;
foreach(var y in serviceSuburbList)
{
    var temp = table.Where(x => x.ServiceId == y.ServiceId && x.SuburbId == y.SuburbId);
    query = query == null ? temp : query.Union(temp);
}

var srtList = query.ToList();

另一個解決方案-使用Z.EntityFramework.Plus.EF6庫:

var srtList = serviceSuburbList.Select(y => 
                 ctx.Customer.DeferredFirstOrDefault(
                    x => x.ServiceId == y.ServiceId && x.SuburbId == y.SuburbId
                 ).FutureValue()
              ).ToList().Select(x => x.Value).Where(x => x != null).ToList();
//all queries together as a batch will be sent to database 
//when first time .Value property will be requested

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM