1

Consider objModels is object of ChildModel class

public class ChildModel
{
public string Title { get; set; }
public long Id { get; set; }
}

It has some data like: title,id: a,1 b,1 a,1

Two of these data are same, and after distinct it could be like: a,1 b,1

Question is how could I distinct it on c#

List<ChildModel> obj= objModels.ToList();

Also these aren't help

objModels.Distinct();
obj.Distinct();
5
  • you have to override Equals and GetHashCode, like this so, msdn Commented May 12, 2017 at 10:24
  • I answered friend dont forget vote up and accept it Commented May 12, 2017 at 10:31
  • @ShahroozAnsari thank you man, but "DistinctBy" isn't exist when I try to use Commented May 12, 2017 at 10:34
  • look at this link msdn.microsoft.com/en-us/library/bb338049.aspx Commented May 12, 2017 at 10:39
  • thank you friend please vote up too :-) Commented May 12, 2017 at 10:58

4 Answers 4

1

You could use a library named MoreLINQ

This is the query you could use with MoreLINQ to find elements that are distinct by multiple properties:

var query = objModels.DistinctBy(p => new { p.Id, p.Title});
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

var answer= obj.DistinctBy(p => new { p.Id , p.Title });

and check this link for other way

1 Comment

DistinctBy requires the MoreLINQ library.
0

I would do the following:

            namespace ConsoleApplication7
            {
                class Program
                {
                    static void Main()
                    {
                        List<ChildModel> list = new List<ChildModel>();
                        list.Add(new ChildModel { Title = "a", Id = 1 });
                        list.Add(new ChildModel { Title = "b", Id = 1 });
                        list.Add(new ChildModel { Title = "a", Id = 1 });

                        var x = list.Distinct(new ChildModelComparer()).ToList();

                        var y = x; //This has only got two child items.

                    }
                }
                class ChildModelComparer : IEqualityComparer<ChildModel>
                {
                    public bool Equals(ChildModel x, ChildModel y)
                    {
                        return x.Id.Equals(y.Id) && x.Title.Equals(y.Title);
                    }

                    public int GetHashCode(ChildModel obj)
                    {
                        if (string.IsNullOrEmpty(obj.Title) && obj.Id == 0)
                        {
                            return 0;
                        }
                        return $"{obj.Id}{obj.Title}".GetHashCode();
                    }
                }
                public class ChildModel
                {
                    public string Title { get; set; }
                    public long Id { get; set; }
                }
            }

1 Comment

I have used IEqualityComparer so that I can use the second overload of distinct.
0

you can use .GroupBy:

var result= obj
  .GroupBy(p => p.Title)
  .Select(g => g.First()) // choose which one
  .ToList();

edit: if you want to GroupBy more than one Property you can just change p.Title to new {p.Title, p.Id} like so

var result= obj
  .GroupBy(p => new {p.Title, p.Id})
  .Select(g => g.First()) // choose which one
  .ToList();

7 Comments

this is so baad way to dothis
This would only Group items by Title, he needs items to be distinct by both Id and Title.
Guys this works on this code, I checking, but on c# I haven't "DistinctBy" during a list like this
@sachin edited my post - was not really clear for me if OP wants more or less properties to be grouped
@Shahrooz Ansari why is this a bad way?
|

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.