Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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
LINQ包含和投影_Linq_Entity Framework - Fatal编程技术网

LINQ包含和投影

LINQ包含和投影,linq,entity-framework,Linq,Entity Framework,我有一些类用关系定义实体 Account has many Conversations [IEnumerable<Conversation> Conversations] Conversation has many Participants [IEnumerable<Account> Participants] has many Messages [IEnumerable<Message> Messages] Message has one Send

我有一些类用关系定义实体

Account 
has many Conversations [IEnumerable<Conversation> Conversations]

Conversation 
has many Participants [IEnumerable<Account> Participants]
has many Messages [IEnumerable<Message> Messages]

Message 
has one Sender [Account Sender]
has one Conversation [Conversation Conversation]
帐户
有很多对话[我有很多对话]
会话
有多个参与者[可数参与者]
有许多消息[IEnumerable消息]
消息
有一个发件人[帐户发件人]
有一个对话[对话]
我正在尝试编写一个LINQ查询,它返回按日期排序的会话列表,包括相关参与者和消息

public async Task<List<Conversation>> FindAllByAccountIdAsync(Int32 id)
{
    return await _Db.Conversations
         .Where(c => c.Participants.Any(p => p.AccountId == id))
         .Include(c => c.Participants)
         .Include(c => c.Messages)
         .ToListAsync();
}
公共异步任务findallbyaccountadasync(Int32 id)
{
返回等待会话
.Where(c=>c.Participants.Any(p=>p.AccountId==id))
.包括(c=>c.参与者)
.Include(c=>c.Messages)
.ToListAsync();
}
这样做的工作,但包括了很多我并不真正需要的数据

public async Task<List<Conversation>> FindAllByAccountIdAsync(Int32 id)
{
    return await _Db.Conversations
         .Where(c => c.Participants.Any(a => a.AccountId == id))
         .Include(c => c.Participants.Select(a=> new 
                       {
                          AccountId = a.AccountId,
                          Profile = new { FullName = a.Profile.FullName,
                                          Email = a.Profile.Email
                                        }                        
                       }))
         // Only return the last message in 
         // Eventually I would not return an array with a single object but just the single object inside associated with the property LastMessageIn
         .Include(c => c.Messages.OrderBy(m => m.Date).Select(m=> new 
                       {
                          Body = m.Body,
                          SenderId = m.Sender.AccountId
                       }).Last())
         .ToListAsync();
}
公共异步任务findallbyaccountadasync(Int32 id)
{
返回等待会话
.Where(c=>c.Participants.Any(a=>a.AccountId==id))
.包括(c=>c.参与者。选择(a=>new
{
AccountId=a.AccountId,
Profile=new{FullName=a.Profile.FullName,
Email=a.Profile.Email
}                        
}))
//仅返回中的最后一条消息
//最终,我不会返回包含单个对象的数组,而只返回与属性LastMessageIn关联的内部单个对象
.Include(c=>c.Messages.OrderBy(m=>m.Date)。选择(m=>new
{
Body=m.Body,
SenderId=m.Sender.AccountId
}).Last())
.ToListAsync();
}
此脚本返回一英里长的异常

{“message”:“出现错误。”,“exceptionMessage”:“包含路径表达式必须引用在类型上定义的导航属性。使用虚线路径引用导航属性,使用Select运算符引用集合导航属性……。}

我的头脑抗拒理解和学习LINQ我不知道这是否只是我自己,但一旦需求超过基本的查询和投影,它就会脱离我的控制


有人有一些提示吗?

您不能在包含上进行投影。包含只是快速加载。C#中的输出不会更改。只有最初加载的数据量(即性能)会更改

看起来您想要的是投影,而不是急于加载,这是完全不兼容的概念


但是,我无法确切理解您想要实现的目标。

您无法在包含上进行投影。包含只是快速加载。输出在C#中不会更改。只有最初加载的数据量(即性能)会更改

看起来您想要的是投影,而不是急于加载,这是完全不兼容的概念


然而,我无法确切理解您想要实现的目标。

我不确定是否理解您的问题,但我相信您希望这样:

public async Task<List<Conversation>> FindAllByAccountIdAsync(Int32 id)
{
    return await _Db.Conversations
         .Where(c => c.Participants.Any(p => p.AccountId == id))
         .Include(c => c.Participants)
         .Include(c => c.Messages)
         .Select(c => new 
         {
            Participants = c.Participants.Select(a=> new 
                       {
                          AccountId = a.AccountId,
                          Profile = new { FullName = a.Profile.FullName,
                                          Email = a.Profile.Email
                                        }                        
                       },
             //EDIT: using OrderByDescending and FirstOrDefault
             Messages = c.Messages.OrderByDescending(m => m.Date).Select(m=> new 
                       {
                          Body = m.Body,
                          SenderId = m.Sender.AccountId
                       }).FirstOrDefault())
             //others properties here
         }
         .ToListAsync();
}
公共异步任务findallbyaccountadasync(Int32 id)
{
返回等待会话
.Where(c=>c.Participants.Any(p=>p.AccountId==id))
.包括(c=>c.参与者)
.Include(c=>c.Messages)
.选择(c=>new
{
参与者=c.参与者。选择(a=>新建
{
AccountId=a.AccountId,
Profile=new{FullName=a.Profile.FullName,
Email=a.Profile.Email
}                        
},
//编辑:使用OrderByDescending和FirstOrDefault
Messages=c.Messages.OrderByDescending(m=>m.Date)。选择(m=>new
{
Body=m.Body,
SenderId=m.Sender.AccountId
}).FirstOrDefault())
//这里的其他物业
}
.ToListAsync();
}

我不确定我是否理解你的问题,但我相信你想要这样的东西:

public async Task<List<Conversation>> FindAllByAccountIdAsync(Int32 id)
{
    return await _Db.Conversations
         .Where(c => c.Participants.Any(p => p.AccountId == id))
         .Include(c => c.Participants)
         .Include(c => c.Messages)
         .Select(c => new 
         {
            Participants = c.Participants.Select(a=> new 
                       {
                          AccountId = a.AccountId,
                          Profile = new { FullName = a.Profile.FullName,
                                          Email = a.Profile.Email
                                        }                        
                       },
             //EDIT: using OrderByDescending and FirstOrDefault
             Messages = c.Messages.OrderByDescending(m => m.Date).Select(m=> new 
                       {
                          Body = m.Body,
                          SenderId = m.Sender.AccountId
                       }).FirstOrDefault())
             //others properties here
         }
         .ToListAsync();
}
公共异步任务findallbyaccountadasync(Int32 id)
{
返回等待会话
.Where(c=>c.Participants.Any(p=>p.AccountId==id))
.包括(c=>c.参与者)
.Include(c=>c.Messages)
.选择(c=>new
{
参与者=c.参与者。选择(a=>新建
{
AccountId=a.AccountId,
Profile=new{FullName=a.Profile.FullName,
Email=a.Profile.Email
}                        
},
//编辑:使用OrderByDescending和FirstOrDefault
Messages=c.Messages.OrderByDescending(m=>m.Date)。选择(m=>new
{
Body=m.Body,
SenderId=m.Sender.AccountId
}).FirstOrDefault())
//这里的其他物业
}
.ToListAsync();
}
公共异步任务findallbyaccountadasync(Int32 id)
{
返回等待会话
.Where(c=>c.Participants.Any(p=>p.AccountId==id))
.Include(c=>c.Participants.Select(=>)
.Include(c=>c.Messages.Select(=>
.ToListAsync();
}
应该足够了。

公共异步任务findallbyaccountadasync(Int32 id)
{
返回等待会话
.Where(c=>c.Participants.Any(p=>p.AccountId==id))
.Include(c=>c.Participants.Select(=>)
.Include(c=>c.Messages.Select(=>
.ToListAsync();
}
应该足够了。

Thx Fabio s