C# Linq Intersect/Except with One Part of Object

C# Linq intersect/except with one part of object

Maybe

// returns list of intersecting property 'a' values
foo.Select(f => f.a).Intersect(bar.Select(b => b.a));

BTW property a should be public.

Low performance with linq and INTERSECT/EXCEPT in small collections

I think it will be faster to use a join and group by like this:

int numberOfFeatures = userToSearch.features.Count();
var userIds = from u in context.Users
from uf in u.UserFeatures
where userToSearch.features.Contains(uf.featuresId)
group u by u.userId into g
where g.Count() == numberOfFeatures
select u.Key;

Alternatively if you want the user objects

var users = ...
select u.FirstOrDefault();

LINQ Set Operations not working (Intersect, Except)

Well, it shouldn't make a difference, but from the point of view of symmetry I'd reverse how you're creating assigned. I'd also make sure that the query is only executed once, and that the remaining operations occur in-process:

var cachedGroups = groups.ToList();
var assigned = cachedGroups.Intersect(user.Groups);
var available = cachedGroups.Except(user.Groups);

One possibility is that user.Groups has a custom equality comparer. That would explain why the foreach version worked but the LINQ version didn't. What's the type of user.Groups, and how much do you know about the equality comparer it's using?

Is sql to Linq union by reference? (the same for except and intersect?)

At this point, .Union is called on .Net collections and its behavior is defined in the docs for Enumerable.Union:

The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface.

This is further clarified in the docs for EqualityComparer<T>.Default:

The Default property checks whether type T implements the System.IEquatable<T> interface and, if so, returns an EqualityComparer<T> that uses that implementation. Otherwise, it returns an EqualityComparer<T> that uses the overrides of Object.Equals and Object.GetHashCode provided by T.

So, if CustomerModel implements IEquatable<CustomerModel>.Equals and/or overrides Object.Equals, then Union uses one of these methods to compare customers.

Otherwise, it falls back on default implementation of Object.Equals that compares object references:

If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method..

Except() and Intersect() doesn't work correctly - LINQ

You need to implement an equality comparer for objects

This has been answered before here
Using Linq Except not Working as I Thought

Please see the blog post below from MSDN for further explanation
http://blogs.msdn.com/b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx

If you are anonymous types it will work differently without an equality comparer. Here is an example code

http://odetocode.com/blogs/scott/archive/2008/03/25/and-equality-for-all-anonymous-types.aspx

Using Include with Intersect/Union/Exclude in Linq

A long time has gone by, but this .Include problem still exists in EF 6. However, there is a workaround: Append every child request with .Include before intersecting/Unionizing.

MyDbContext C = new MyDbContext();
var queryOne = db.Parents.Where(p => p.Name == parent.Name).Include("Children");
var queryTwo = db.Parents.Where(p => p.Location == parent.Location).Include("Children");
var finalQuery = queryOne.Intersect(queryTwo);

As stated by @Ivan Stoev, Intersection/Union is done with after-fetched data, while .Include is ok at request time.

So, as of now, you have this one option available.

why doesn't .Except() and Intersect() work here using LINQ?

public int GetHashCode(Item obj)
{
return obj.Id.GetHashCode();
}

Worth a check at least -- IIRC GetHashCode() is tested first before equality, and if they don't have the same hash it won't bother checking equality. I'm not sure what to expect from obj.GetHashCode() -- it depends on what you've implemented on the Item class.

Is the order of an except and intersect operation always ignorable?

From a purely set-theoretic perspective the order does not matter here. In both steps you're just removing elements from list. An element is removed from the list if it's either in anotherList or not in yetAnotherList. Whether you switch the operands of the »or« in the previous sentence doesn't make a difference.

To illustrate, let's construct three lists (sets here, because those are set operations):

list = { A, B, C, D }
anotherList = { A, B, E, F }
yetAnotherList = { A, C, E, G }

Each set here has one element that is in all three sets (A), one element that is in each of the other sets, and one element that is only in that set but not in the others. So we have all possible cases for intersection and set difference covered here.

list.Except(anotherList) yields { C, D }. Intersecting that with yetAnotherList yields { C }.

list.Intersect(yetAnotherList) yields { A, C }. Excepting anotherList yields { C } again.

Linq Select List if Records present in another List

You want the intersection:

var list3 = list1.Intersect(list2).ToList();


Related Topics



Leave a reply



Submit