Monday 15 September 2014

linq - List with two columns - using partial search -



linq - List with two columns - using partial search -

i have list 2 columns , trying utilize contains search partially. emp contains id , name.

var list = new list<emp>(); list = list.any(o => o.contains(new emp{ name = name }));

however throwing error: cannot convert emp system.linq. iqueryable

any input on helpful

thanks in advance

it's not clear you're trying do. might want:

list = list.where(emp => emp.name == name).tolist();

or perhaps

list = list.where(emp => emp.name.contains(name)).tolist();

it's not @ clear why you'd trying utilize any though.

edit: given comment, suspect want:

list = list.where(emp => emp.name.startswith(name)).tolist();

(you might need create case-insensitive though.)

or if need check each part of name, either utilize regular look or:

list = list.where(emp => emp.name.split(' ') .any(x => x.startswith(name))).tolist();

this lastly approach really inefficient though, split each name each time through it... potentially create emp objects immutable , cache split form. or regular look version. either way, don't want string.contains find matches anywhere in name.

linq list

No comments:

Post a Comment