2

快速提问:我正在将 EF4 EntityCollection 中的实体 ID 与循环中的简单 int[] 进行比较。我想做类似的事情:

for (int i = 0; i < Collection.Count; ++i)
{
    Array.Any(a => a.value == Collection[i].ID) ? /* display yes */ : /* display no */;
}

我只是不确定如何将数组中的值与 EntityCollection 中的值进行比较,或者换句话说,使用什么来代替我上面编写的 value 属性。

4

2 回答 2

1

跳过循环,你可以做这样的事情

array.Any(a => collection.Any(c => c.ID == a)) ? /* display yes */ : /* display no */;

Any()如果您需要循环,那么您可以从上面跳过第二个并执行

array.Any(a => collection.ElementAt(i).ID == a) ? /* display yes */ : /* display no */;
于 2011-06-10T21:03:48.077 回答
1

代码应修改为:

int[] arr = //this is the integer array
IEnumerable Collection = //This is your EF4 collection
for (int i = 0; i < Collection.Count; ++i)
{
    arr.Any(a => a == Collection[i].ID) ? /* display yes */ : /* display no */;
}

我在顶部列出了一些变量,以便我们清楚什么是什么。改变的主要部分是,Array.Any我们不是在调用,而是在调用arr.Any. Any是一个扩展方法int[],因此您在数组本身上调用它,而不是在类上Array

这能解决问题吗?

于 2011-06-10T21:05:26.890 回答