gpt4 book ai didi

c# - 如何在 LINQ 查询的 where 子句中调用方法/函数作为 IEnumerable 对象

转载 作者:行者123 更新时间:2023-11-30 13:55:29 24 4
gpt4 key购买 nike

<分区>

我在 linq 查询中的 where 子句中使用函数时遇到问题,如下面的代码所示。

  1. 返回IEnumerable对象的函数:

    private IEnumerable filterparameter(string filter) 
    {
    EmailAflAwmMessageDM obj = new EmailAflAwmMessageDM();

    if (filter == "attachments")
    {
    return obj.attachments;
    }
    else if (filter == "flagged")
    {
    return obj.flagged;
    }
    else
    {
    return obj.seen;
    }
    }
  2. 返回 IQuerable 的函数,将在 web api 函数中使用:

    private IQueryable SearchFilterCondition(string filter,string value)
    {
    var emailmessage = from a in db.EmailAflAwmMessage
    where (filterparameter(filter) == value)
    orderby a.msg_date descending
    select new
    {
    a.subject,
    a.msg_date,
    };
    return emailmessage;
    }

更新:这个错误

{"Message":"An error has occurred.",
"ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":null,
"InnerException": {
"Message":"An error has occurred.",
"ExceptionMessage":"LINQ to Entities does not recognize the method 'System.Collections.IEnumerable filterparameter(System.String)' method, and this method cannot be translated into a store expression.",
"ExceptionType":"System.NotSupportedException",
"StackTrace":"

请帮助我解决此问题或指导我使用替代方法。

24 4 0