Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I had a list contains holidayname and date.Only one holiday can be specified on a day
I had done this in for loop

for (int i = 0; i < holidaylist.Count; i++)
          {
              for (int j = i + 1; j < _holidayviews.Count; j++)
                  {
                      if (holidaylist[i].Date == holidaylist[j].Date)
                      {
                          throw new Exception("Holiday Date cannot be same");
                      }
                  }



it is working but i need to use linq query..
can any help me to get out of this problem..
Posted

1 solution

It can be something like:


C#
if (holidaylist.Select(h => h.Date).Distinct().Count() != holidaylist.Count)
{
    throw new Exception("Holiday Date cannot be same");
}
 
Share this answer
 
v2
Comments
kevin_ze 12-Jul-12 2:49am    
Thanks a lot Shmuel Zang its working Fine but am not able to understand the flow of that code can u explain that if u wish to
Shmuel Zang 12-Jul-12 2:59am    
We made a collection of dates. Then, we used the Distinct method to remove duplicates. Then, we compared the count of the created collection with the count of the original collection (holidaylist).
kevin_ze 12-Jul-12 3:03am    
Thank u very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900