Question

Quick question: I'm comparing the IDs of entities in an EF4 EntityCollection against a simple int[] in a loop. I'd like to do something like:

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

I'm just not sure how to compare the value within the array with the value from the EntityCollection, or, in other words, what to use for real instead of the value property I made up above.

Was it helpful?

Solution

The code should be modified to read:

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 */;
}

I called out a few variables at the top just so we're clear as to what is what. The major part that changed was that instead of invoking Array.Any we're invoking arr.Any. Any is an Extension method for int[] and thus you invoke it on the array itself, not on the class Array.

Does this solve the problem?

OTHER TIPS

skip the loop and you can do something like this

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

if you need the loop then you can skip the second Any() from above and do

array.Any(a => collection.ElementAt(i).ID == a) ? /* display yes */ : /* display no */;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top