简体   繁体   English

在Entity Framework中使用行集合中的where

[英]Use where in row collection in Entity Framework

I have below classes : 我有以下课程:

  1. News 新闻
  2. PostPermission 邮政许可
  3. Role 角色

each News Have ListOfPostPermissions(1-many) and each PostPermission have one Role (1-1). 每个新闻都有ListOfPostPermissions(1-many),每个PostPermission具有一个Role(1-1)。

I have List of RoleId and I want to get all News where PostPermission 's RoleId is in the List of RoleId . 我的名单RoleId ,我想所有的News ,其中PostPermissionRoleId是在列表RoleId

I use below code but It throws error : 我使用以下代码,但会引发错误:

var roles = _currentUserService.GetCurrentUserRoles(); // List<Guid>
return NewsList.Where(row => row.Permissions.Where(role=>roles.Contains( role.RoleId)).ToList()).ToList();

I getting these errors: 我收到这些错误:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'bool' 无法将类型'System.Collections.Generic.List'隐式转换为'bool'

Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type 无法将lambda表达式转换为委托类型'System.Func',因为该块中的某些返回类型不能隐式转换为委托返回类型

how can I do it ? 我该怎么做 ?

You need to use Any inside, instead of Where like this 您需要使用Any内部,而不是Where这样的

return NewsList
    .Where(row => row.Permissions.Any(role=>roles.Contains(role.RoleId)))
    .ToList();

This is because the outer Where expects a predicate, ie expression that returns a bool . 这是因为外部Where期望谓词,即返回bool表达式。 This is clearly indicated by the first exception message. 第一个异常消息清楚地表明了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM