There is a way to define that all Table1 element that i select, it has a Table3 property filled based in Table3ElementName property, without create a new class or use anonymous type when i select it? (in .edmx or in code with a linq query)
I know that is possible to do this with a join like that:
var tables1 =from t1 in context.Table1 selectnew { Id= t1.Id, ElementName= t1.ElementName, Table3ElementName= t1.Table3ElementName, Table3=(from t3 in context.Table1 where t3.ElementName== t1.Table3ElementName select t3).FirstOrDefault() }
The problem with this approach is that i have to create a new type, but i have to keep the Table1 object. Is this possible?
var tables1 = from t1 in context.Table1
select new
{
Id = t1.Id,
ElementName = t1.ElementName,
Table3ElementName = t1.Table3ElementName,
Table3 = (from t3 in context.Table1
where t3.ElementName == t1.Table3ElementName
select t3).FirstOrDefault()
}
When i use anon type, the returned type to tables1 is not a Table1, but i need that returned type to tables1 be a Table1, for exemple a IQueryble<Table1> or IEnumerable<Table1>.
var tables1 = from t1 in context.Table1
select new
{
Id = t1.Id,
ElementName = t1.ElementName,
Table3ElementName = t1.Table3ElementName,
Table3 =
(
from t3 in context.Table1
where t3.ElementName == t1.Table3ElementName select t3).FirstOrDefault())
};
I think if you are using FirstOrDefault,then the value for Table3 will be Table1 instead of IEnumerable<Table1>;So please remove FirstOrDefault and have a try……
Regaurds!
The problem is not with Table3 (property of Table1) but with the query. Doing "select new { }" the return is a new type, a anon type, not a Table1 type. And what i need is a Table1 type in return of the query.
Vecthor
Member
19 Points
27 Posts
Create a relationship in entity framework that doesn't exist in database, is it possible?
Apr 19, 2012 06:40 PM|LINK
Let's understand first the database scenario. I have the following tables:
The default Entity Framework Mapping will be:
Now what i want. I want to create a property in Table1 that have a logical relationship with Table3, so, the Table1 will be:
There is a way to define that all Table1 element that i select, it has a Table3 property filled based in Table3ElementName property, without create a new class or use anonymous type when i select it? (in .edmx or in code with a linq query)
I know that is possible to do this with a join like that:
The problem with this approach is that i have to create a new type, but i have to keep the Table1 object. Is this possible?
entity-framework Linq
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
Apr 21, 2012 01:33 AM|LINK
Hello Vecthor:)
In fact I think if you are using EF(Code-First),I think you can just refer Table3 as a virtual class type:
public class Table1
{
……………………
public virtual Table3 Table3{get;set;}
}
Vecthor
Member
19 Points
27 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 02, 2012 11:16 AM|LINK
My problem is not with the refer, but with the query. This way to refer, i did, like i said in the question.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 03, 2012 01:11 AM|LINK
What does that mean:Create a new type?
What's that type?
And keep the Table1 object——What's that?
Can you elebrate it more?
thanks anyway
Vecthor
Member
19 Points
27 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 07, 2012 12:33 PM|LINK
What does that mean:Create a new type?
Use anon type:
var tables1 = from t1 in context.Table1 select new { Id = t1.Id, ElementName = t1.ElementName, Table3ElementName = t1.Table3ElementName, Table3 = (from t3 in context.Table1 where t3.ElementName == t1.Table3ElementName select t3).FirstOrDefault() }When i use anon type, the returned type to tables1 is not a Table1, but i need that returned type to tables1 be a Table1, for exemple a IQueryble<Table1> or IEnumerable<Table1>.
Thanks for help!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 08, 2012 01:19 AM|LINK
Hello Vecthor:)
I think if you are using FirstOrDefault,then the value for Table3 will be Table1 instead of IEnumerable<Table1>;So please remove FirstOrDefault and have a try……
Regaurds!
Vecthor
Member
19 Points
27 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 08, 2012 11:48 AM|LINK
The problem is not with Table3 (property of Table1) but with the query. Doing "select new { }" the return is a new type, a anon type, not a Table1 type. And what i need is a Table1 type in return of the query.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 09, 2012 01:06 AM|LINK
OK……So according to your description,I think you can say——
select new Table1
{
Property1 = Value1,
Property2 = Value2,
………………
}
Vecthor
Member
19 Points
27 Posts
Re: Create a relationship in entity framework that doesn't exist in database, is it possible?
May 15, 2012 02:14 PM|LINK
No, i discovered that i cannot do it. With Linq-to-entities is not possible to do this.