简体   繁体   English

LINQ实体

[英]Linq to entities

I was wondering how i compared to an array of ids in EF1.0 我想知道如何与EF1.0中的ID数组进行比较

I know in EF4.0 Beta 1 there is a contains methods so it would look something like this 我知道在EF4.0 Beta 1中有一个contains方法,因此它看起来像这样

int[] associationIds = GetAssociationIds();
from c in Associations where c.AssociationId.Contains(associationIds) select c;

But how do you do the equivalent in EF1.0 但是,您如何做EF1.0中的等效功能

There's no built-in way to do this in EF1. EF1中没有内置方法可以做到这一点。 The most commonly used tool for this task is PredicateBuilder . 用于此任务的最常用工具是PredicateBuilder

The solution (using that toolkit) is to build a custom expression that tests AssociationId against each of the ids in your integer array. 解决方案(使用该工具包)是构建一个自定义表达式,以针对整数数组中的每个ID测试AssociationId The resultant expression looks something like this: 结果表达式看起来像这样:

int[] associationIds = GetAssociationIds();

// use PredicateBuilder to build this expression using the contents of
// associationIds:

Expression<Func<Association, bool>> testIds = 
    c => c.AssociationId == 1 || c.AssociationId == 2 || c.AssociationId == 5;

And to use it: 并使用它:

var matchingAssociations = db.Associations.Where(testIds);

The predicatebuilder documentation lists a way to do it without the linqkit so i used that method, its not pretty but it will do the job until EF4.0 comes along. predicatebuilder文档列出了一种无需linqkit即可完成此操作的方法,因此我使用了该方法,虽然效果不佳,但在EF4.0出现之前它可以完成工作。 Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM