edtijgen (1) [Avatar] Offline
#1
Hi All,
Please consider the following database model


Boy (table name)
pk_BoyId (field)
Name (field)

BoyGirl (table name)
fk_BoyId (field)
fk_GirlId (field)

Girl (table name)
pk_Girl (field)
Name (field)

When using the ADO.NET Entity Framework model wizard on this database model the result is 2 entities

Boy (Entity)
pk_BoyId (Member)
Name (Member)
Girl (navigation property)

Girl (Entity)
pk_GirlId (Member)
Name (Member)
Boy (navigation property)


Now How can I create a collection of girls to use in a join on Boy.
So I'd like to get every Boy that has a relation to a girl from the collection of girls I created seperatly from the database.
Something like this:

List<Girl> girls = new List<Girl>();
Girl girl = new Girl();
girl.GirlId = 1;
girl.Name = "Paula";
girls.Add(girl);
girl = new Girl();
girl.GirlId = 3;
girl.Name = "Debby";
girls.Add(girl);

var boys = from b in context.Boy
join g in girls on b.girl == q

select b;
foreach (Boy resultBoy in boys)
{
Console.WriteLine(resultBoy.Name);
}


But this does not work, i am unsure how to proceed
fabrice.marguerie (224) [Avatar] Offline
#2
Re: Simple LINQ join?
Hi Ed,

Could you try with the following and let us know if it works?

var girlIds =
from girl in girls
select girl.GirldId;
var boys =
from boy in context.Boy
where girlIds.Contains(boy.Girl.GirlId)
select boy;

It should work with LINQ to SQL. Maybe with the ADO.NET Entity Framework as well?

Fabrice
fabrice.marguerie (224) [Avatar] Offline
#3
Re: Simple LINQ join?
FYI, an answer has been provided by Microsoft in the official LINQ forum here: http://forums.microsoft.com/MSDN/showpost.aspx?postid=2212577&siteid=1