Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
SQL
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
Comments
Gopinath_Rajan 21-Mar-13 3:42am    
Gothrough this
http://www.codeproject.com/Tips/298963/Understand-Lambda-Expressions-in-3-minutes

Do you need the collection of Lst1 values that are not in Lst2?
Try something like:
C#
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:
C#
var L1notL2 = Lst1.Any(l1 => !Dict2.ContainsKey(l1.ID));

These both should be O(n + m) where n = Lst1.Count and m = Lst2.Count.
 
Share this answer
 
v2
C#
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.
 
Share this answer
 
v4
Comments
Matt T Heffron 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 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 26-Mar-13 13:31pm    
That's exactly correct.
Please check this.

SQL
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 };
 
Share this answer
 
Comments
Matt T Heffron 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.
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

C#
            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>
 
Share this answer
 
Comments
Matt T Heffron 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.
Try this:

C#
var r = from t in lst1 where !lst2.Exists(i => i.ID == t.ID) select t;


considering lst1 and lst2 is something like following:

C#
public class Item
{
   public int ID;
}

List<item> lst1 = new List<item>();
List<item> lst2 = new List<item>();
 
Share this answer
 
v2
Comments
Matt T Heffron 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 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 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 26-Mar-13 1:21am    
Ok - agreed! ContainsKey optimizes the performance and it would make more sense when you are dealing with large collections;
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;
 
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