2

I have a list of users as given below:

List<User> users = new List<User>();

users.Add(new User(){ UserId = "11", City = "London" });
users.Add(new User(){ UserId = "12", City = "London" });
users.Add(new User(){ UserId = "12", City = "London" });
users.Add(new User(){ UserId = "11", City = "Newyork" });
users.Add(new User(){ UserId = "14", City = "Virginia" });

Here, I want to get distinct UserIDs those have different City by C# lambda expression

So, in above case I should get a List<string> which will only contains UserId = 11 item because UserId is same but city is different for both the item.

Could you please let me know how would I do this by C# lambda code.

Thanks in advance.

4
  • 4
    What have you tried? Commented Aug 29, 2012 at 11:00
  • @SynerCoder I tried by GroupBy lambda clause, but not succeed. Commented Aug 29, 2012 at 11:02
  • @nunu: Please show your code. Commented Aug 29, 2012 at 11:03
  • Here is similar question: stackoverflow.com/questions/1183403/… Commented Aug 29, 2012 at 11:03

3 Answers 3

8

Something like:

var result = users.GroupBy(u => u.UserId)
                  .Where(g => g.Select(u => u.City).Distinct().Count() > 1)
                  .Select(g => g.Key)
                  .ToList();

should do it.

It takes the {UserId,City} pairs and converts into groups of those pairs indexed by UserId; and then looks for cases where there is more than one city in the group. Finally taking the key from the groups for the result.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Richard... Now I want to get reverse scenarios... I want to select only those UserIds whose have single or multiple SIMILAR cities... Could you please tell me how would I do it?? Thanks in advance!
@nunu For the single city change the > 1 condition to == 1. For similar you'll need to replace the Distinct with something that works with whatever you mean by similar (this might be a custom IEqualityComparer<T> passed to Enumerable.Distinct's overload). Without a specific definition of similar it is too ambiguous to be more specific.
Richard do you have an example of how to pass a lambda/anonymous function to Enumerable.Distinct? I.e. like this but without the separate method.
@dumbledad Unfortunatelly you cannot. Enumerable.Distinct takes a IEqualityComparer<T> rather than a delegate. And there is no way to create an implementation of an interface with a delegate (whether another function or lambda): you have to have a type. It should be possible to create a helper type that implements IEqualityComparer<T> based on a key extraction delegate without too much difficulty (but too long to write here with the limited formatting options available).
1
from u in users group u.City by u.UserId into grp //group on the id
where grp.Distinct().Count() > 1 // we only want those with more than one distinct city
select grp.Key //we want the key

1 Comment

Your answer is absolutely perfect !! Just the different is Lambda and LINQ :) I upvoted too..
0
var list = users.GroupBy(Obj=>new {Obj.UserID,Obj.City}).Distinct();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.