【发布时间】:2020-05-16 04:34:32
【问题描述】:
我完全理解这是因为 LINQ 查询需要将整个表达式转换为服务器,因此我不能在其中调用外部方法。但是当我查看其他答案时,没有相对的解决方案。我想到的唯一方法是遍历模型中的所有项目,然后将它们逐个传递给查询,但即使这种方法无济于事,所以我在这里寻求帮助,让任何人帮助我弄清楚如何调用方法或调用方法的方式 appendstr 初始化 a.PostedDate 在检查其实际等效值在给定的 LINQ 查询之前。
[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 查询中调用的第二种方法
public string appendstr(string str)
{
var x = str.Split(' ');
return 01 + "-" + x[1] + "-" + x[2];
}
【问题讨论】:
-
你想用这个
appendstr(a.PostedDate).Equals(now.AddHours(-24).ToString("dd-MM-yyyy"))做什么? PostedDate 的格式是什么? -
抱歉回复晚了 2020 年 1 月 30 日上午 10:23
-
MM-dd-yyyy 还是 dd-MM-yyyy?如所写,您将保留发布的日期和年份,并将月份强制为一月。这就是你想要的吗?
标签: asp.net asp.net-mvc linq