多输入匿名函数到表达式以在 LINQ 中使用

Multiple input anonymous function to expression for use in LINQ

我正在尝试开发一个 LINQ 查询,它将测试数据库中的字符串是否在没有子字符串返回 true 的数组中,只返回整个字符串。

Func<int, string[], bool> predicate = (i, x) => x.Any(i.ToString().Equals);

                user.Company += (from c in context.Tbl_Institute
                                 where (predicate(c.Institute_ID,values))
                                 select c.Institute_Title + ","
                                ).ToString();

到目前为止我有这个可以编译但是给我一个错误: LINQ to Entities 不支持 LINQ 表达式节点类型 'Invoke'。

从研究来看似乎意味着我需要使用一个表达式。

但是,这不会编译。

Expression`<Func<int, string[], bool>> predicate = (i, x) => x.Any(i.ToString().Equals);`

As predicate(c.Institude_ID,values) 读取错误 "Method name expected".

有没有人有这方面的经验?我对匿名函数很陌生。

编辑:这里要求的是无法编译的代码,如果我不清楚,我深表歉意。

Expression<Func<int, string[], bool>> predicate = (i, x) => x.Any(i.ToString().Equals);
user.Company += (from c in context.Tbl_Institute
                 where (predicate(c.Institute_ID,values))
                 select c.Institute_Title + ",").ToString();

在这里,您的要求 Entity Framework 有点过分了。我不明白你为什么要在 LINQ 查询之外保留那个匹配函数,你能解释一下你为什么这样做吗?我认为 EF 在这方面遇到了困难。

这是一段我认为应该有效的代码:

List<string> values = new List<string> { "1", "2", "3", "4" };

var matchingInstitutesNames = context
    .Tbl_Institute
    .Where(x => values.Contains(x.Institute_Id.ToString()))
    .Select(x => x.Institute_Title)
    .ToList();

var joinedInstitutesNames = string.Join(",", matchingInstitutesNames);
user.Company += joinedInstitutesNames;

如果这不起作用,您可能希望将 string 列表转换为 int 列表,这样 EF 就不必从 int 进行转换至 string.