【问题标题】:filter linq to sql with a entity framework where in使用实体框架将 linq 过滤为 sql,其中
【发布时间】:2015-05-05 03:04:47
【问题描述】:

我正在做 2 个 linq 查询,一个是实体框架,另一个是 linq to sql,它们根本不能很好地配合使用。

第一个查询通过实体框架获取一个表中的 id。

var pemdata = from pd in db.tblMap 
                          where pd.PID == pid
                          select new 
                          {
                              eid = pd.EID
                          };

然后我使用包含通过 linq to sql 过滤第二个查询。

var data = from e in p.entities 
                       join et in p.entity_types on e.entity_type equals et.entity_types_id

                       where pemdata.Contains(e.entity_id) 

                       select new gEntities
                       {
                           entity_type = e.entity_type.ToString(),
                           Name = e.entity_name,
                           Type = et.entity_types_name,
                           Address = e.entity_address,
                           City = e.entity_city,
                           Zip = e.entity_zip.ToString()
                       };

我看到的问题是一个错误。

实例参数:无法从 'System.Linq.IQueryable' 转换为 'System.Linq.ParallelQuery'

'System.Linq.IQueryable' 不包含 'Contains' 的定义和最佳扩展方法重载 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' 有一些无效参数

我通常不会遇到这样的问题。但是当我将 linq 混合到 sql 和实体框架时,我遇到了这个问题。

有什么想法吗?

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    您需要在使用结果之前具体化第一个查询。最简单的方法是只调用 .ToList() 就可以了。然后您可以在第二个查询中使用结果。

    var pemdata = (from pd in db.tblMap 
                              where pd.PID == pid
                              select pd.EID).ToList();
    

    【讨论】:

    • 我已经尝试过 ToList()。但它给出了一个不同的错误。参数 1:无法从 'int' 转换为 'AnonymousType#1' 并且 'System.Collections.Generic.List.Contains(AnonymousType#1)' 的最佳重载方法匹配有一些无效参数
    • 让我们尝试删除匿名类型 -- 更新答案。
    • 你一针见血。匿名类型导致了这个问题。先生,您是一位绅士和一位学者。
    • 也许是学者,另一个有待商榷。
    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2020-06-20
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多