Reputation: 293
I am trying to fill a Rights object in my PersonRight object using the following code:
var results = (from P in this.ObjectContext.Person
join R in this.ObjectContext.Rights
on R.personid = p.personid
select new PersonRight(){
Name = P.Name,
}).AsQueryable();
public class PersonRight
{
public property Person { get; set; }
public List<Rights> Rights { get; set; }
}
I would like to populate the Rights object with this query, how can this be done? I can not use entity framework created entity so I need to fill these database objects into a custom type.
Thank you
Upvotes: 1
Views: 948
Reputation: 177133
You could try this:
var results = (from P in this.ObjectContext.Person
join R in this.ObjectContext.Rights
on P.personid equals R.personid
into PR
select new PersonRight()
{
Name = P.Name,
Rights = PR
}).AsQueryable();
public class PersonRight
{
public string Name { get; set; }
public IEnumerable<Rights> Rights { get; set; }
}
A few things to notice:
equals
in LINQ join
and not ==
.equals
must be the outer variable, so on R.personid equals P.personid
is wrong.into
keyword to create a group of all rights for a particular person. The variable PR
represents an IEnumerable of all rights of the person.Rights
in your PersonRight
class is a List<Rights>
. Using PR.ToList()
is not possible in LINQ to Entities because it will complain that it cannot translate ToList()
to a store expression.Person.Rights
). If you have it you should leverage it in your query and don't use join
.Upvotes: 1
Reputation: 1562
var results = (from P in this.ObjectContext.Person
select new PersonRight()
{Person = P,
Rights = p.Rights.ToList() }).ToList();
OR
var results = (from P in this.ObjectContext.Person
select new PersonRight()
{Person = P,
Rights = this.ObjectContext.Rights.Where(r=>r.personid == p.personid ).ToList() }).ToList();
Upvotes: 0
Reputation: 7612
One possible solution (maybe not very effective?) is to simply get list of Persons
and in creation of each PersonRights
object, get corresponding Rights
:
var results =
(from P in this.ObjectContext.Person
select new PersonRight
{
Name = P.Name,
Rights = (from R in this.ObjectContext.Rights where R.personid == P.personid select R).ToList()
}).AsQueryable();
Upvotes: 0