Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make Boolean value as true if the main list does not contain newly added 2nd items list.
static void Main()
       {
           List<int> toUserIds = new List<int>() { 1,3,4,5};
           List<int> fromUserIds = new List<int>() { 6,1,4,3,5};
           bool isMDNUpdated = false;
           foreach (int userID in fromUserIds)
           {
               if (toUserIds.IndexOf(userID) == -1)
               {
                   isMDNUpdated = true;
                   break;
               }
           }

           Console.WriteLine(isMDNUpdated);
           Console.Read();
       }


What I have tried:

I have tried with the below code.
Could you please correct me if this code is not in proper way.
static void Main()
       {
           List<int> toUserIds = new List<int>() { 1,3,4,5};
           List<int> fromUserIds = new List<int>() { 6,1,4,3,5};
           bool isMDNUpdated = false;
           foreach (int userID in fromUserIds)
           {
               if (toUserIds.IndexOf(userID) == -1)
               {
                   isMDNUpdated = true;
                   break;
               }
           }

           Console.WriteLine(isMDNUpdated);
           Console.Read();
       }
Posted
Updated 29-Aug-18 18:21pm

Well ... you can do it in a single line:
C#
bool hasNew = fromUserIds.Except(toUserIds).Count() != 0;
 
Share this answer
 
Comments
Herman<T>.Instance 29-Aug-18 5:18am    
Both Except and Intersect are powerful and valuable LINQ methods.

Solution 1 is correct but there is no need to instantiate an enumerable and then count it. You can get an early exit when the first difference is encountered by using the Any extension.


C#
bool isUpdated = fromUserIds.Any(i => !toUserIds.Contains(i));
 
Share this answer
 

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