LINQ to Entities无法识别其中的方法调用 [英] LINQ to Entities does not recognize the method call within it

查看:71
本文介绍了LINQ to Entities无法识别其中的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全理解这是因为LINQ查询需要将整个表达式转换为服务器,因此我无法在其中调用外部方法.但是当我查看其他答案时,还没有相对的解决方案.我想到的唯一方法是遍历模型中的所有项目,而不是将它们逐个传递给查询,尽管这种方法无济于事,所以我在这里寻求帮助,以帮助任何人找出答案如何在初始化LINQ查询中检查它的实际等效值之前,调用用于初始化 a.PostedDate 的方法或方法 appendstr 的方法.

I completely understand that this is because LINQ query requires the whole expression to be translated to a server , and therefore I cant call an outside method in it. but as I have looked at other answers there is not relative solution to this. the only approach that I thought about is to loop through all the items in the model and than passing them to the query one by one but even though this approach would not help so I am seeking help in here for anyone to help me to figure out how to call a method or a way of calling a method appendstr that initializes a.PostedDate before checking its actual equivalent value in the giving LINQ Query.

[HttpGet]
public ActionResult SearchResult(int? page, string searchTitle = null, string searchLocation = null, string last24 = "")
{


    ViewBag.searchTitle = searchTitle;
    ViewBag.searchLocation = searchLocation;
    ViewBag.page = page;
    ViewBag.last24 = last24;

    setUpApi(searchTitle, searchLocation);
    var result = new List<AllJobModel>().AsQueryable();
    if (!string.IsNullOrEmpty(ViewBag.searchTitle) || !string.IsNullOrEmpty(ViewBag.searchTitle) || !string.IsNullOrEmpty(ViewBag.last24))
    {
        setUpApi(searchTitle, searchLocation);
        DateTime now = DateTime.Now;

        result = db.AllJobModel.Where(a => a.JobTitle.Contains(searchTitle) && a.locationName.Contains(searchLocation) &&
        appendstr(a.PostedDate).Equals(now.AddHours(-24).ToString("MM-dd-yyyy")));
    }
    else
    {
        result = from app in db.AllJobModel select app;
    }


    return View(result.ToList().ToPagedList(page ?? 1, 5));
}

在LINQ查询中调用的第二种方法

The second method that gets called in the LINQ Query

public string appendstr(string str)
{
    var x = str.Split(' ');

    return  01 + "-" + x[1] + "-" + x[2];
}

推荐答案

我认为您已经了解,您在 Where 子句中编写的.NET代码实际上是一个经过解析并转换为的表达式SQL.因此,如果您有一种时髦的字符串操作方法,则不能直接使用它.

I think you already understand that the .NET code you write in the Where clause is actually an expression that is parsed and converted to SQL. So if you have a funky string manipulation method, you can't use it directly.

您似乎已经了解,蛮力选项将首先实现查询,然后对结果运行C#代码.您可以使用 ToList() AsEnumerable().

The brute force option, as you seem to already understand, it to materialize the query first and then run the C# code over the results. You can do this with ToList() or AsEnumerable().

result = db.AllJobModel
    .Where
    (
        a => a.JobTitle.Contains(searchTitle)
          && a.LocationName.Contains(searchLocation)
    )
    .AsEnumerable()
    .Where
    (
        a => appendstr(a.PostedDate).Equals(now.AddHours(-24).ToString("MM-dd-yyyy")))
    );

但是,在您的特定情况下,您可以尝试技巧.您正在尝试进行日期比较,这是SQL完全有能力执行的操作……您只需要将时髦的 PostedDate 转换为SQL DateTime,以便您可以直接进行比较.为此,可以使用 SqlFunctions.DateAdd 添加空间隔(例如0天).这会将字符串隐式转换为DateTime,现在您可以在SQL端进行查询:

However in your specific case you can try a trick. You are attempting a date comparison, which SQL is perfectly capable of doing... you just need to convert that funky PostedDate to a SQL DateTime so that you can compare it directly. The gimmick for that is to use SqlFunctions.DateAdd to add null interval (e.g. 0 days). This implicitly converts the string to DateTime, where you can now query on the SQL side:

var targetDate = DateTime.Now.AddHours(-24);
result = db.AllJobModel
    .Where
    (
        a => a.JobTitle.Contains(searchTitle)
          && a.LocationName.Contains(searchLocation)
          && SqlFunctions.DateAdd("DAY", 0, a.PostedDate) == targetDate
    );

信用转到这篇文章解决方法.

这篇关于LINQ to Entities无法识别其中的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆