Saint
Saint

Reputation: 5469

Getting collection from foreign key

In such a two tables

table Person
{
    int Id -> primary key
    varchar name
    varchar nick
    int GroupId -> foreign key
}

table Group
{
    int Id -> primary key
    varchar name
}

If I use

var result = (from c in myDataBase.Group
             select c).ToList<Group>();

I get only list of Group, but field System.Data.Objects.DataClasses.EntityCollection<Person> is empty. How should I change query to get also list of Persons?

Upvotes: 0

Views: 214

Answers (2)

Saint
Saint

Reputation: 5469

I solved problem with:

myDataBase.Group.Include("Person").Select(a => a).ToList();

Btw: what is equivalent of Include() in linq query from...where...select ?

Upvotes: 0

Tigran
Tigran

Reputation: 62246

It's not clear where System.Data.Objects.DataClasses.EntityCollection<Person> has to be, but I presume you are searching for Inner join

  var query = from person in people
                join group in groups on person.GroupId equals group .Id
                select new {.. something... };

Here I presumed you have somewhere people (collection of Person types) and want to find all persons from that collection and their matching information from related Group.

If this is not what you're asking for, please clarify.

Upvotes: 1

Related Questions