如何在 LINQ to Entities 查询中调用我的函数

发布于 2025-01-01 07:29:44 字数 1555 浏览 3 评论 0原文

问题

我使用 linq 返回匿名对象列表

from s in _subsidiaryService.Repository.Query()
   where  s.Title.ToLower().Contains(term) ||
   s.Description.ToLower().Contains(term) ||
   s.District.ToLower().Contains(term)
   select new
              {
                  label = s.Title
                  ,
                  desc = s.Description
                  ,
                  value = s.ID
                  ,
                  icon = Url.Content(Path.Combine(string.Format(Constants.RelativePathSubsidiary, s.ID), SmallIconFileName))  //Problem line!!
              }

icon 行是问题所在

Linq to Entities 无法识别 Path.Combinestring.Format< /code>

问题

我有两个问题:

  1. 如何在查询中运行 Path.Combine string.format
  2. 如何创建在查询中运行的函数?例如:

代码示例

from s in _subsidiaryService.Repository.Query()
   where  s.Title.ToLower().Contains(term) ||
   s.Description.ToLower().Contains(term) ||
   s.District.ToLower().Contains(term)
   select new
              {
                  label = s.Title
                  ,
                  desc = s.Description
                  ,
                  value = s.ID
                  ,
                  icon = getIconPath(s.SmallIconFileName)
              }

我的方法

public string getIconPath(Guid id, sring fileName) {
    return Url.Content(Path.Combine(string.Format(Constants.RelativePathSubsidiary, id), fileName));
}

Problem

I use linq to return a list of anonymous objects

from s in _subsidiaryService.Repository.Query()
   where  s.Title.ToLower().Contains(term) ||
   s.Description.ToLower().Contains(term) ||
   s.District.ToLower().Contains(term)
   select new
              {
                  label = s.Title
                  ,
                  desc = s.Description
                  ,
                  value = s.ID
                  ,
                  icon = Url.Content(Path.Combine(string.Format(Constants.RelativePathSubsidiary, s.ID), SmallIconFileName))  //Problem line!!
              }

The line icon is where the problem is

Linq to Entities does not recognize Path.Combine or string.Format

Questions

I have two questions:

  1. How can I run and Path.Combine string.format inside my queries
  2. How do I create functions that run inside my queries? e.g:

Code sample

from s in _subsidiaryService.Repository.Query()
   where  s.Title.ToLower().Contains(term) ||
   s.Description.ToLower().Contains(term) ||
   s.District.ToLower().Contains(term)
   select new
              {
                  label = s.Title
                  ,
                  desc = s.Description
                  ,
                  value = s.ID
                  ,
                  icon = getIconPath(s.SmallIconFileName)
              }

My method

public string getIconPath(Guid id, sring fileName) {
    return Url.Content(Path.Combine(string.Format(Constants.RelativePathSubsidiary, id), fileName));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2025-01-08 07:29:44

将其更改为:

(from s in _subsidiaryService.Repository.Query()
where  s.Title.ToLower().Contains(term) ||
s.Description.ToLower().Contains(term) ||
s.District.ToLower().Contains(term)
select s).AsEnumerable().Select(s => new { label = s.Title, desc = s.Description, value = s.ID, icon = getIconPath(s.SmallIconFileName) });

.AsEnumerable() 将其从查询切换到内存操作,因此它不会尝试将函数映射到数据库。

值得注意的是,这意味着它会在数据库上运行 .AsEnumerable 之前的所有内容,将其本地拉到 IEnumerable,然后开始迭代它以创建匿名对象。因此,这样做会对性能产生影响。通过使用匿名对象或确保所有 Where 过滤在 AsEnumerable 之前完成,可能值得修剪您选择的列。

Change it to:

(from s in _subsidiaryService.Repository.Query()
where  s.Title.ToLower().Contains(term) ||
s.Description.ToLower().Contains(term) ||
s.District.ToLower().Contains(term)
select s).AsEnumerable().Select(s => new { label = s.Title, desc = s.Description, value = s.ID, icon = getIconPath(s.SmallIconFileName) });

The .AsEnumerable() switches it from the query to in memory operations, so it doesn't try to map the functions to the database.

Its worth noting though that this means it runs everything up to the .AsEnumerable on the database, pulls it locally to an IEnumerable then starts iterating it to do the anonymous object creation. So there are performance implications to doing this. It may be worthwhile to trim the columns you are selecting by using an anonymous object or making sure all of your Where filtering is done prior to the AsEnumerable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文