views:

120

answers:

5

Hi,

I have an array of strings

var controlsToGet = new[] {"lblHome","lblContact"};

I have List<LanguageControl> and LanguageControl class holds Controls in it. I want to get Controls from List which Control.Name == controlsToGet

I am looking for something like that

var all = fooelements.where(l=>l.Control.Name == controlsToGet);

Is it possible with lambda or linq.

Note: I was able to do it with Nhibernate's Disjunction, i am looking for something like that

EDIT : What would i do if i want to use this query for data base with the Entity Framework ?

Thank you

+9  A: 
var all = fooelements
              .Where(l=>controlsToGet
                            .Contains(l.Control.Name));


If each item has a list of controls:

var all = fooelements
              .SelectMany(l => l.Controls)
              .Where(c => controlsToGet
                             .Contains(c.Name));
Martin Harris
Thank you, i don't have much exp with lambdas.Now, i got the way how to handle it
Barbaros Alp
I short lambda could be great to ... var result = fooelements.FindAll(item=> controlsToGet.Contains(item.Name));
Cédric Boivin
I don't mind the lose in reputation, but I wish whoever has downvoted had posted a comment. I'd be interested in knowing what was wrong with this answer even if it was only to help me educate myself...
Martin Harris
Agree with your comment
Cédric Boivin
+1 Harris, am i able to use it with Entity framework ? I have tried it and it didnt work
Barbaros Alp
Sorry, I've never used the Entity Framework. If there is a specific issue with this code and the Entity Framework you may be best asking a new question with some code samples of exactly what you're trying to do.
Martin Harris
Thank you, i' ll do it
Barbaros Alp
+1  A: 
var all = from control in listofcontrols
          from toGet  in controlsToGet
          where toGet == control.name
          select control;
Preet Sangha
+2  A: 

If controlsToGet is a string, this will work:

var all = fooelements.SelectMany(l => l.Controls).Where(c => c.Name == controlsToGet);

However, if it's a List<string> you will need:

var all = fooelements.SelectMany(l => l.Controls).Where(c => controlsToGet.Contains(c.Name));
Jason
+3  A: 

You could also use the findall. If microsoft optimise the method you will increase performance, if not it's will be the same as the foreach

var result = fooelements.FindAll(item=> controlsToGet.Contains(item.Name));
Cédric Boivin
+1  A: 

A naïve solution such as:

fooelements.SelectMany(l => l.Controls).Where(c => controlsToGet.Contains(c.Name))

is O(n*m) where n is the number of controls, and m is the number of strings in your name array. Some may argue that this is nitpicking and YAGNI and ... (yadda yadda), but what the heck - just a little more code gives you an O(n) solution:

var controlsToGet = new HashSet<string> { "lblHome", "lblContact" };
var controls = fooelements.SelectMany(l => l.Controls)
    .Where(c => controlsToGet.Contains(c.Name))
Eric Smith
Good point. Assuming there are enough comparisons to overcome the initial cost of making the hashes this should be a faster solution.
Martin Harris
Yes, the main point being that you've eliminated a performance dependency on the number of control names (which will no doubt change).
Eric Smith