Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 在Linq to实体查询中包含不相关的表_C#_Linq_Include_Linq To Entities_Entity - Fatal编程技术网

C# 在Linq to实体查询中包含不相关的表

C# 在Linq to实体查询中包含不相关的表,c#,linq,include,linq-to-entities,entity,C#,Linq,Include,Linq To Entities,Entity,我有以下简化设置: Public User { //Primary key public int Id {get;set;} public string Name {get; set;} } Public UserInfo { //Primary key public int Id {get;set;} //Foreign key to user table public int userKey {get; set;} } 表之间的关系是一对多userIn

我有以下简化设置:

Public User
{

  //Primary key
  public int Id {get;set;}

  public string Name {get; set;}
}

Public UserInfo
{

  //Primary key
  public int Id {get;set;}

  //Foreign key to user table
  public int userKey {get; set;}
}
表之间的关系是一对多userInfo

我试图从
user
表中进行选择,并包括
userInfo

我不能这样做:

var users = Context.user.Include(u => u.userInfos);
因为用户表中没有对
UserInfo
表的引用

我可以这样做:

context.userInfo.include(x => x.user)
但是如果userInfo表中没有相应的条目,这将不会返回任何结果,这不是我想要的。此外,这将为每个userInfo返回一行,而我希望为user返回一行,并将userInfo列表作为参数

类似地,我可以这样连接表:

var users = from us in Context.user
    join inf in Context.userInfo
    on us.Id equals inf.userKey
    select new //etc
但这也将为每个userInfo条目返回一行,与上面的问题相同

总之,是否有一种方法可以包含此表,以与
include
函数相同的方式生成结果

我知道我可以调整我的设置,以包括所有我,但这不是我在这里要求的

我怀疑这是不可能做到的,但从我所有的谷歌搜索到目前为止,我还没有找到一个明确的答案

我想要一行user,并将userInfo列表作为参数

我想你指的是作为属性的用户信息列表。我对您所要求的内容的理解是:

var users = from us in Context.user
    join inf in Context.userInfo
        on us.Id equals inf.userKey into infg
    select new
    {
        User = us,
        UserInfos = infg
    };
加入。。。into
相当于一个,即与一组userinfos连接的用户实体

更好的方法是使用导航属性
user.userInfos
(不情愿地遵循命名约定):


我猜
User
有一个属性
UserInfos
或者类似的东西?(顺便说一句,帮你自己一个大忙,遵守命名约定,比如以大写字母开头的类和属性名)。那么你在寻找linq中的左连接?这是一个解决方案,我编辑了这个问题来解释为什么我不能使用你建议的include方法,但是你建议的第一个解决方案是有效的,所以谢谢你!!
var users = Context.user.Include(u => u.userInfos);