Intersect Linq Query

Intersect LINQ query

Yes.

As other people have answered, you can use Where, but it will be extremely inefficient for large sets.

If performance is a concern, you can call Join:

var results = original.Join(idsToFind, o => o.Id, id => id, (o, id) => o);

If idsToFind can contain duplicates, you'll need to either call Distinct() on the IDs or on the results or replace Join with GroupJoin (The parameters to GroupJoin would be the same).

Intersection of two lists using LINQ

EDIT:
People in the comments are taking about overriding equals for your Company object and they are correct however we might be able to do something easier. The reason you need to override equals is because .Net doesn't know how to find equality in an object you created. so you would need to program in how to let it know. It does know how to find equality in an ID most times however.

EDIT 2:
The Intersect Any is the way to go because you are want to compare a list to a list

public List<User> GetUsers(User admin)
{
var adminCompanyIDs = admin.Companys.Select(c => c.ID);
return Users.Where(user=>user.Companys.Select(c => c.ID).Intersect(adminCompanyIDs).Any()).ToList();
}

So Contains will search the list to see if any single value are in the list. Because it only searches for a single value it won't work for this.

Intersect will return the intersections of the two lists. for example [1,2,3] [2,3,4] would give [2,3].

Where requires a boolean for the function evaluation. Give me the values in my list where the function given returns true. So when you give back [2,3] it complains. Any says are there any results in the list. so [2,3].Any() returns true, satisfying the Where.

Contains doesn't return the Intersection of the list, just tells you True of False, Does the value exist

Hope that helps.

Linq optimized intersect query

Arkiliknam's solution is pretty efficient, maybe the most efficient one, but it's got some issues. I started to point them out in a comment, to allow the answer to be improved, but that didn't work well. So here is Arkiliknam v. 2.0:

var result = context.Vacancies; // IQueryable!
var tags = userSelectedTags.Select(t => t.TagId);

foreach(int i in tags)
{
int j = i; // prevent modified closure.
result = result.Where(v => v.Tags.Select(t => t.TagId).Contains(j));
}
result.Select(r => ....

It is converted to query with a chain of WHERE EXISTS predicates (as many as there are selected tags).

If this does not perform well you could try another approach:

var r = context.Vacancies
.Where(v => tags.All(i => v.Tags.Select(t => t.TagId).Contains(i)))
.Select (v => ....);

which says that for all selected TagIds each Id should be in a Vacancy's collection of TagIds.

Linq Intersect with partial match

Why would you need intersect? Just use an Any Contains.

Prices.Where(x => sParams.Any(s => x.Color.ToUpper().Contains(s)));

what is the mechanism for performing an intersect in a Mongo Linq query

In MongoDB syntax there's a $in operator which works exactly like intersect+any when you want to match an in-memory array with another array embedded in your document.

If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (e.g. , , etc.)

In MongoDB C# driver you can use AnyIn to apply that operator for two arrays. Try:

db.col.save({ Collection: [1,2,3] })l

Then in C#:

var filterBuilder = Builders<YourModel>.Filter;
var inMemoryList = new List<int>() { 3, 4, 5 };

var result = Col.Find(filterBuilder.AnyIn(x => x.Collection, inMemoryList)).ToList();


Related Topics



Leave a reply



Submit