Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - Distinct by value?

Tags:

c#

.net

linq

Code :

news = (from New myNew in new News()
       select myNew).Distinct().ToList();

but this Distinct is for "object" with same values. I need, into my list, a myNew for each month. (so one for january, one for februaru, and so on). Than, news will get 12 record.

Is it possible a sort of Distinct(myNew.Month)?

like image 248
markzzz Avatar asked May 09 '12 10:05

markzzz


People also ask

How to Distinct LINQ c#?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).

Why distinct is not working in Linq?

Yes, it doesn't work as expected! This is because the Distinct method uses the default equality comparer to compare the values under the hood. Since we are dealing with reference type object, the Distinct() method will treat the values as unique even if the property values are the same.

How does Linq distinct work?

LINQ Distinct operator removes all the duplicate values from the collection and finally returns the dissimilar or unique values. The LINQ Distinct operator available in only Method Syntax and it not supports the Query Syntax. LINQ Distinct is an operator which comes under Set Operator.


2 Answers

You could group by month and take the first or last or whatever(you haven't told us):

var news = News()
           .GroupBy(n => n.Month)
           .Select(grp => grp.Last());

Edit: From the comment on Habib's answer i see that you want 12 months even if there are no news. Then you need to do a "Linq Outer-Join":

var monthlyNews = from m in Enumerable.Range(1, 12) // left outer join every month
                  join n in News() on m equals n.Month into m_n
                  from n in m_n.DefaultIfEmpty()
                  group n by m into MonthGroups
                  select new {
                      Month = MonthGroups.Key, 
                      LastNews = MonthGroups.Last() 
                  };
foreach (var m in monthlyNews)
{
    int month = m.Month;
    var lastNewsInMonth = m.LastNews;
    if (lastNewsInMonth != null) ; // do something...
}

Edit: Since you have problems to implement the query in your code, you don't need to select the anonymous type which contains also the month. You can also select only the news itself:

var monthlyNews = from m in Enumerable.Range(1, 12) // every motnh
                  join n in news on m equals n.Month into m_n
                  from n in m_n.DefaultIfEmpty()
                  group n by m into MonthGroups
                  select MonthGroups.Last();  

Note that you now get 12 news but some of them might be null when there are no news in that month.

like image 167
Tim Schmelter Avatar answered Oct 09 '22 16:10

Tim Schmelter


var result  =  News()
  .GroupBy(p => p.Month)
  .Select(g => g.First())
  .ToList();
like image 24
Habib Avatar answered Oct 09 '22 16:10

Habib