Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从ef相关实体获取属性_C#_Entity Framework - Fatal编程技术网

C# 如何从ef相关实体获取属性

C# 如何从ef相关实体获取属性,c#,entity-framework,C#,Entity Framework,使用下面的常规sql语句 select b.*, a.name from table_b b left join table_a a on a.id = b.aid public class A { [key] public int id {get;set;} public string name {get;set;} .... } public class B{ [key] public int id {get;set;} public string name {get;set;} [For

使用下面的常规sql语句

select b.*, a.name from table_b b
left join table_a a on a.id = b.aid
public class A {
[key]
public int id {get;set;}
public string name {get;set;}
....
}
public class B{
[key]
public int id {get;set;}
public string name {get;set;}
[ForeignKey("a")]
public in aid {get;set;}
[NotMapped]
public string A_name{get{return this.a.name;}}
public virtual A a {get;set;}
...
}
在ef中,我构建了两个相关实体,如下所示

select b.*, a.name from table_b b
left join table_a a on a.id = b.aid
public class A {
[key]
public int id {get;set;}
public string name {get;set;}
....
}
public class B{
[key]
public int id {get;set;}
public string name {get;set;}
[ForeignKey("a")]
public in aid {get;set;}
[NotMapped]
public string A_name{get{return this.a.name;}}
public virtual A a {get;set;}
...
}
我的dbcontext代码在这里

mycontext.set<B>()
.Include(T => T.a)
.Where(.....).FirstOrDefault();
mycontext.set()
.包括(T=>T.a)
.Where(…).FirstOrDefault();
它对我有用,但看起来很愚蠢,好像B类有很多相关的键 与cid、did、eid等一样,一条记录将占用许多数据,ef中也是如此 EXEPY sqlQuery还有其他像noraml sql这样的简单方法来获取数据吗 来自其他实体的额外财产?
顺致敬意,非常感谢

您可以编写linq查询:

var result= ( from a in mycontext.DBSetA_Name
              join b in mycontext.DBSetA_Name
              on a.id equals b.id
              into res
              from c in res.DefaultIfEmpty()
              select new {a,c}
             )
             .Select(x=> new
                         {
                           IdA= x.a.id,
                           NameA=x.a.name,
                           NameB=x.c==null?null:x.c.name
                         }).ToList();

结果将包含sql查询将给出的确切结果集。

谢谢Sumit raj,我曾经尝试过这个mycontext.set()。其中(…).select(T=>new{T,A_Name=T.A.Name})。FirstOrDefault();它也可以工作,但需要另一个函数将A_名称放入T,如果B有许多相关的id,那将是噩梦它不工作吗?我写这篇文章是因为我只想要特定的属性,而不是整个A。在使用ef之前,我使用ado.net获取datatable,然后使用entityconverter函数convert to custome entity。遇到ef后,我是ef的初学者,所以我认为强大的ef可以满足我的要求。。。