Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Linux C#4.0
Hello,
i have 2 list which contains values as ID's.
Lst1
Lst2
 
in my lamba expression i want to check if any Lst1 is not available in Lst2 items.
 
i am trying a join :
from item in Lst2
                                join lblId in Lst1 on item.ID equals lblId.id
                                where !string.IsNullOrEmpty(item.ID) && item.ID != lblId.id
 
But yhis doesnot work .
Any suggestion Please help!
Posted 20-Mar-13 22:17pm
Comments
Gopinath_Rajan at 21-Mar-13 3:42am
   
Gothrough this
http://www.codeproject.com/Tips/298963/Understand-Lambda-Expressions-in-3-minutes
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Do you need the collection of Lst1 values that are not in Lst2?
Try something like:
var Dict2 = Lst2.ToDictionary(l => l.ID);
// var L1notL2 = Lst1.Where(l1 => !Dict2.ContainsKey(l1.ID)).Select(l => l);
var L1notL2 = Lst1.Where(l1 => !Dict2.ContainsKey(l1.ID));  // The .Select() above is redundant!
If all you need is a boolean that some entry of Lst1 does not have a matching ID with any entry of Lst2, then change the second line above to:
var L1notL2 = Lst1.Any(l1 => !Dict2.ContainsKey(l1.ID));
These both should be O(n + m) where n = Lst1.Count and m = Lst2.Count.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

            List<int> first = new List<int>()
            {
                1,2,3,4,5,6,7,8
            };
 
            List<int> second = new List<int>()
            {
                1,2,3,4,
            };
 
            List<int> result = first.Where(x => !second.Any(y => y == x)).ToList<int>();
            
            //Second approach I found after little googling:

            List<int> result2 = first.Except(second).ToList();            
 
 
This example takes every int ID in list1(first) which is not contained in list2(second).
My second example uses Except method which has very good performance.
  Permalink  
v4
Comments
Matt T Heffron at 22-Mar-13 16:45pm
   
This looks like it would be O(n*m), where n and m are the length of the two lists...see my solution #2.
D.Nikolov at 26-Mar-13 9:21am
   
Matt I am wondering what does ToDictionary. I suppose it iterates through the collection to generate dictionary. Do you know what ToDictionary does?
Matt T Heffron at 26-Mar-13 13:31pm
   
That's exactly correct.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please check this.
 
from x in Lst1
                      join x1 in Lst2 on x.ID equals x1.ID into res1
                      from r in res1.DefaultIfEmpty()
                      where r == null
                      select new { id = x.ID };
  Permalink  
Comments
Matt T Heffron at 21-Mar-13 16:31pm
   
This looks like it would be O(n*m), where n and m are the length of the two lists...see my solution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I agree with Solution 2. Make story short, write such code, which can be understanble.
 
You can use try something like below given
 
I have created dummy two list. you can implement your own one
 
 
            List<id> list1 = new List<id>();        
            //Wherer ID is Class containing only id property
            list1.Add(new ID() { id=1});
            list1.Add(new ID() { id = 2 });
 
            List<id> list2 = new List<id>();
            list2.Add(new ID() { id = 1 });
            list2.Add(new ID() { id = 2 });
            list2.Add(new ID() { id = 3 });
 
            List<int> ids = list1.Select(x=>x.id).ToList();
            var items = (from s in list2 where !ids.Contains(s.id) select s).ToList();
 
</int></id></id></id></id>
  Permalink  
Comments
Matt T Heffron at 22-Mar-13 16:45pm
   
This looks like it would be O(n*m), where n and m are the length of the two lists...see my solution #2.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Try this:
 
var r = from t in lst1 where !lst2.Exists(i => i.ID == t.ID) select t;
 
considering lst1 and lst2 is something like following:
 
public class Item
{
   public int ID;
}
 
List<item> lst1 = new List<item>();
List<item> lst2 = new List<item>();
  Permalink  
v2
Comments
Matt T Heffron at 22-Mar-13 16:45pm
   
This looks like it would be O(n*m), where n and m are the length of the two lists...see my solution #2.
RajeshRaushan at 25-Mar-13 3:04am
   
RajeshRaushan - 1 sec ago
Hey Matt. I feel both are in Order(M * N)
In the Solution 2 also if you see - the statement
!Dict2.ContainsKey(l1.ID) is actually in Order of N
then,
Lst1.Where is in Order of M
so when it runs then for each of M the N will be applicable
so it is also of O(M*N)
The same applies to
var r = from t in lst1 where !lst2.Exists(i => i.ID == t.ID) select t;
also. Here just instead of ContainsKey i have used Exists.
There is no difference otherwise.
Matt T Heffron at 25-Mar-13 13:08pm
   
The Dict2.ContainsKey is NOT O(N), it is O(1)!!!
 
The Dictionary class implments a hash-based lookup so it has approximately constant time, independent of the number of items in the Dictionary, to see if the ContainsKey is true. (See http://msdn.microsoft.com/en-us/library/kw5aaea4.aspx )
 
This is why I proposed the Dictionary, because it is more efficient than a simple scan of a list.
RajeshRaushan at 26-Mar-13 1:21am
   
Ok - agreed! ContainsKey optimizes the performance and it would make more sense when you are dealing with large collections;
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Linq is general purpose query facility which we can write query on any object
 
example Employes record:
 
Employee emp = from empobj in EmployeeRecord where empobj.Name.equal("Manish") select empobj;
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 288
1 ProgramFOX 165
2 Peter Leow 130
3 Sergey Alexandrovich Kryukov 95
4 Fredrik Bornander 85
0 OriginalGriff 288
1 ProgramFOX 165
2 Peter Leow 130
3 Sergey Alexandrovich Kryukov 95
4 Fredrik Bornander 85


Advertise | Privacy | Mobile
Web03 | 2.8.150301.1 | Last Updated 26 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100