开发者

How can I Check for item existence in 2 observableCollection linq

Just a bit rusty on the old linq. If I have 2 collections EG NewCustomerList and OldCustomerList and see if a surname exists already how would I do it in linq .I am sure there are many ways. SelectMany rings a bell but forgot how to do it!

In a forEach I would do something like that. What is the equivalent in linq?

     foreach (var  oldCustomer in OldCustomerList)
     {
        foreach (var newCustomer 开发者_StackOverflowin NewCustomerList.Where(x => x.Surname == oldCustomer.Surname))
        {
           break;
        }
     } 

Any Suggestions? Thanks a lot


So you're trying to see whether any of the old customer surnames are in the new customer list?

One simple option: do a join and see if it's empty:

if (OldCustomerList.Join(NewCustomerList, x => x.Surname, x => x.Surname,
                         (x, y) => null).Any())
{
    ...
}

(I've used a null projection because we really don't care about the join result.)

Another option:

var oldSurnames = new HashSet<string>(OldCustomrList.Select(x => x.Surname));
if (NewSurnameList.Any(x => oldSurnames.Contains(x.Surname))
{
    ...
}

I suspect you may find that you actually want the result in terms of which surnames are in common though... if you can give us more context, we may be able to help you more.


You can do this by:

NewCustomerList.Where(n => OldCustomerList.Any(o => o.Surname == n.Surname))


Here's another approach, which also has O(n + m) complexity + quick-pass semantics:

OldCustomerList.Select(cust => cust.Surname)
               .Intersect(NewCustomerList.Select(cust => cust.Surname))
               .Any();

IMO, it is more readable than an explicit Enumerable.Join: "test if the projection of surnames from the old customer-list has any elements in common with the projection of surnames from the new customer-list", which is pretty close to the problem-statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜