I'm trying to find the best approach to filtering a list with C# (3.5) but I can't seem to find any examples that are similar to my situation. I'm open to using lambdas or linq.
The thing that is unique compared to most of the examples that I've found, is that my list items each have child arrays for example...
var employees= new List<Employee>
{
new Employee{Name = "John",Nicknames="'J','Big J','Big John'"},
new Employee{Name = "Joshua",Nicknames="'J','Josh','Joshman'"},
}
I'd then like to filter that list something like this...
//using linq
var matchesByNickname =
from worker in employees
where worker.Nicknames.Equals("J")
select worker;
//or lambda
var employees2 = employees
.Where(e => Nicknames.Exists(n => n.Nickname == "J"))
But of course since Nicknames is itself an array I cannot use .Equals or .Contains etc. What would be the best approach to filtering a list of this type?
UPDATE: In trying to keep my example simple I've misled you a bit. The list items do have true object arrays on them, not strings. 开发者_如何学CMy real world example is a list of custom product objects. The product object has a Regions property on it which is a list of Region objects. A product can have none, 1, or more than 1 region. The region object has a name and an id. So what I really want is to filter a list of products, for any product assigned to a certain region.
In your example, Nickname
is not an array as I see it. It's a string. Assuming it's an array of strings, you could:
var employees2 = employees.Where(e => e.Nicknames.Contains(name));
If you want to keep it as is (string), you can try something along the lines of:
var employees2 = employees.Where(e => e.Nicknames.Split(',')
.Contains("'" + name + "'"));
In both of the cases above, you can replace Where
with FindAll
and get results back in a List<T>
instead of an IEnumerable<T>
(List<T>.FindAll
has been around since 2.0 but Enumerable.Where
is new in 3.5).
Of course, ideally, you'll make it an array or list like the following and use the first method:
var employees = new List<Employee>
{
new Employee{Name = "John",Nicknames= new [] {"J", "Big J", "Big John"}},
new Employee{Name = "Joshua",Nicknames = new [] {"J", "Josh", "Joshman"}},
};
UPDATE: Your real world example is not so much different from this sample case. I think you're looking for Any
instead of Contains
since you are checking a property, not the whole object for equality:
var filteredProducts = products
.Where(p => p.Regions.Any(r => r.Name == "regionToSearchFor"));
If you have the single quotes around the nicknames, you could just peek into the string for the single quoted name.
employees.Where(x=>x.Nicknames.IndexOf("'J'") >= 0)
Edit: Forgot single quotes...and >=
精彩评论