Reputation: 1607
I have two lists of diferrent types which has a common guid i need to compare on .
if the value exists in both of the lists I need to populate an isPresent property in a third list.
class a {
Guid stub;
string name;
int number;
}
class b {
Guid SecondStub;
.
.
}
class temp
{
bool isPresent;
string somethingElse;
}
------------------------------------------
now I have one List each of type a and b and I need to populate an isPresent property in a third List.
foreach (var a in ListofA)
{
temp.Add(new Temp(){
isPresent = ListOfB.Where(l => l.SecondStub == a.Stub).Equals(null)
})};
is not working .. please help.
Upvotes: 1
Views: 126
Reputation: 15865
In linq syntax it should look like this:
var temps = from a in ListofA
join b in ListofB on a.SecondStub == b.Stub
select new temp { isPresent = True,
OtherProperty = "something",
ThirdProperty = "something else"};
Though you've not left any place in temp to say which one was true, only the isPresent
flag.
Perhaps you mean to include the value in a property in temp?
Upvotes: 1
Reputation: 22945
The reason Where is not working is that Where always returns an object, since it returns an enumerable, and possibly with no items in it, but it is still not null.
Use Any instead of Where.
var c = a
.Select(x => new Temp {
IsPresent = b.Any(z => z.SecondStub == x.Stub)
})
.ToList();
(from my phone, so formatting might be off)
Upvotes: 0