【问题标题】:Filter LINQ to entities query based on 2 dimensional array基于二维数组过滤 LINQ 到实体查询
【发布时间】:2011-05-12 03:59:22
【问题描述】:

这是一个 Linq2entities 挑战...

我有一个实体(ID、CategoryID、Value)和一个带有 CategoryID/Value 对的二维 int 数组。我需要按每对过滤所有实体,例如:

from e in Entity
where (e.CategoryID and e.Value) in array
select e;

所以基本上是一个“两个链接字段”过滤器。

一个肮脏的解决方案是连接和比较,例如:

concatarray = some function to concat CategoryID + "/" + Value; 

from e in Entity
where e.CategoryID + "/" + e.Value in concatarray
select e;

但由于性能问题,我不想使用它。

有什么想法吗?

非常感谢!

【问题讨论】:

    标签: c# linq entity-framework linq-to-entities


    【解决方案1】:

    首先,我会将您的数组转换为具有特定属性的对象列表。为此使用二维数组不是一个好主意。

    那么查询,可能无法在 EF 中翻译成 SQL。

    from e in Entity
    where array.Where(a=>a.CategoryID == e.CategoryID && a.Value == e.Value).Any()
    select e
    

    【讨论】:

    • +1 但为了简洁起见更喜欢.Any(a=>a.CategoryID == e.CategoryID && a.Value == e.Value)
    • 没用的家伙:“无法创建类型为 'GEO.Data.CategoryAttributeValue' 的常量值。只有原始类型......” List attributes = new List( ); attributes.Add(new CategoryAttributeValue { CategoryAttributeID = 1, Value = 1 }); var queryAttributes = from a in db.CategoryAttributeValues where attributes.Where(av => av.CategoryAttributeID == a.CategoryAttributeID).Any() select a; queryAttributes.ToList();
    猜你喜欢
    • 1970-01-01
    • 2016-02-27
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    相关资源
    最近更新 更多