6

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)?

Cœur
  • 37,241
  • 25
  • 195
  • 267
markzzz
  • 47,390
  • 120
  • 299
  • 507
  • If you have multiple records for a month, how do you want decide which one to take? The first one perhaps? – Jamiec May 09 '12 at 10:14
  • 1
    Is `News` a collection of `New` elements? – Steven May 09 '12 at 10:15
  • @Danny there's no overload of Distinct that takes a lambda – Rune FS May 09 '12 at 10:21
  • Well, the answer to the question I added gives myCustomerList.GroupBy(cust => cust.CustomerId).Select(grp => grp.First()); – Danny Varod May 09 '12 at 10:25
  • @Perception: This answer is not a duplicate at all from ["Distinct() with lambda?"](http://stackoverflow.com/questions/1300088/distinct-with-lambda) as you can see easily from [my answer](http://stackoverflow.com/a/10514121/284240). – Tim Schmelter May 10 '12 at 10:41
  • Does this answer your question? [LINQ's Distinct() on a particular property](https://stackoverflow.com/questions/489258/linqs-distinct-on-a-particular-property) – Magnetron Jun 01 '21 at 12:09

5 Answers5

15

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
6

Solution 1. Get MoreLinq (also available as NuGet package and use

  News().DistinctBy(n => n.Property)

Solution 2. Implement an IEqualityComparer and use this Distinct() overload.

georgiosd
  • 3,038
  • 3
  • 39
  • 51
5
var result  =  News()
  .GroupBy(p => p.Month)
  .Select(g => g.First())
  .ToList();
Habib
  • 219,104
  • 29
  • 407
  • 436
2

Short hand solution

var vNews  =  News()   
              .GroupBy(p => p.Month, (key, p) => p.FirstOrDefault()) 
              .ToList();
Arasu RRK
  • 1,078
  • 16
  • 28
0
var vNews  =  News()   
              .GroupBy(p => p.Month)   
              .Select(g => g.First())   
              .ToList();
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154