views:

31

answers:

1

Using LINQ to entities, I'm trying to filter a list based on another list.

My first list looks like:

IQueryable<FooViewModel> allFoos = from foo in ctx.Foo 
                                   select new FooViewModel 
                                   {
                                     code = foo.Code,
                                     text = foo.Text,
                                     ...
                                   };

My second list looks like:

IQueryable<FooViewModel> myBars = from bar in ctx.Bar.Include("Baz") select bar

There are multiple bazes per bar, and baz has Code and Text properties.

I want to filter allFoos so that I get back only those elements where Code == baz.Code && text == baz.Text

A: 

You can group selects like this:

var query = from x in foos
            from a in bars
            where x.Name == a.Name
            select new FooViewModel { code = x.Code, text = x.Text };

You can also of course nest different queries, like replace from a in bars with from a in barQuery.ToList().

Filip Ekberg
chris
Sweet! Don't forget to accept answers if you think they are correct / solved your problem, i see that you only accepted half of your questions.
Filip Ekberg
Apparently I ask tough questions :)
chris
Hehe might be so yes :)
Filip Ekberg