Newblie Linq Question - Intersecting to lists on partial matches
- I'm pretty new with Linq so forgive me if this is one of those "well, duh!" questions.
I have an IEnumerable<string> where each string represents someone's name in the format Lastname, Firstname.
I have another IEnumber<string> where each string represents a Last name
I'd like to get an IEnumerable<string> list of people's name whos last name are in my last names collection.
For example:
var LastNames = new [] {"Smith","Jones"}
var People = new [] {"Smith, Ted", "Jones, Bob", "Jones, Alan", "Kent, Clark"}
I'd like to end up with a list/array/Ienumerable containing "Smith, Ted", "Jones, Bob", and "Jones, Alan".
The intersect operator seems close, but I believe it does full string matching.
Thanks.
Answers
Hi Chris,
As you said, the Intersect use a full string match, so we cannot use it in such scenario unless we customize an IEqualityComparer(string).
Two string comparison methods for your references:
================================================================
var query = People.Where(p => LastNames.Contains(p.Split(',')[0]));var query2 = People.Where(p => LastNames.Contains(p.Substring(0, p.IndexOf(','))));
================================================================Kristofer, for your method, how about if we have a LastNames array : {“Smith, Jone”} ? J
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byLingzhi SunMSFT, ModeratorWednesday, November 11, 2009 10:10 AM
- Proposed As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:37 AM
- Ah, ok, I see what you mean.
from p in people
from l in lastNames
where p.StartsWith(l + ",")
select p
..then...
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)- Proposed As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:37 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorWednesday, November 11, 2009 10:09 AM
All Replies
- from p in people
from l in lastNames
where p.StartsWith(l)
select p
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro) Hi Chris,
As you said, the Intersect use a full string match, so we cannot use it in such scenario unless we customize an IEqualityComparer(string).
Two string comparison methods for your references:
================================================================
var query = People.Where(p => LastNames.Contains(p.Split(',')[0]));var query2 = People.Where(p => LastNames.Contains(p.Substring(0, p.IndexOf(','))));
================================================================Kristofer, for your method, how about if we have a LastNames array : {“Smith, Jone”} ? J
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byLingzhi SunMSFT, ModeratorWednesday, November 11, 2009 10:10 AM
- Proposed As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:37 AM
Kristofer, for your method, how about if we have a LastNames array : {“Smith, Jone”} ? J
That's why there's a join hidden in there... :)
from p in people
from l in lastNames
where p.StartsWith(l)
select p
...will do an inner join between the people and lastname arrays, with the join criteria that the returned 'people' elements start with the lastname element that it is joined to.
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)Hi Kristofer,
I don't quite understand the INNER JOIN here. Can you explain more? I used the LastNames array: { "Smith", "Jone" }, but the query returns three people, "Smith, Ted", "Jones, Bob", "Jones, Allen". I think it should only return "Smith, Ted".Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Ah, ok, I see what you mean.
from p in people
from l in lastNames
where p.StartsWith(l + ",")
select p
..then...
Kristofer - Huagati Systems Co., Ltd.
Cool tools for Linq-to-SQL and Entity Framework:
huagati.com/dbmltools (add-in with new features for Visual Studio 2008's L2S and EF designers)
huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)- Proposed As Answer byLingzhi SunMSFT, ModeratorMonday, November 09, 2009 12:37 AM
- Marked As Answer byLingzhi SunMSFT, ModeratorWednesday, November 11, 2009 10:09 AM
Hi Chris,
How is the problem now? If you need any further assistance, please feel free to let me know.Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Thanks for the response. You've answered my question.

