12,403,295 members (72,949 online)
Rate this:
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 :
```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 .
Posted 20-Mar-13 21:17pm
Gopinath_Rajan 21-Mar-13 3:42am

Gothrough this
http://www.codeproject.com/Tips/298963/Understand-Lambda-Expressions-in-3-minutes

Rate this:

## 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`.
v2
Rate this:

## 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.
v4
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.
Rate this:

## Solution 1

```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 };```
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.
Rate this:

## 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 = 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>```
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.
Rate this:

## 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>();```
v2
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;
Rate this:

## 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;

Top Experts
Last 24hrsThis month
 Karthik Bangalore 360 OriginalGriff 298 Richard MacCutchan 181 Suvendu Shekhar Giri 175 Richard Deeming 173
 OriginalGriff 7,311 ppolymorphe 3,080 Karthik Bangalore 3,012 Richard MacCutchan 2,300 F-ES Sitecore 2,187