如何在 LINQ to Entities 查询中调用我的函数
问题
我使用 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.Combine
或 string.Format< /code>
问题
我有两个问题:
- 如何在查询中运行 Path.Combine string.format
- 如何创建在查询中运行的函数?例如:
代码示例
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:
- How can I run and Path.Combine string.format inside my queries
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改为:
.AsEnumerable()
将其从查询切换到内存操作,因此它不会尝试将函数映射到数据库。值得注意的是,这意味着它会在数据库上运行
.AsEnumerable
之前的所有内容,将其本地拉到IEnumerable
,然后开始迭代它以创建匿名对象。因此,这样做会对性能产生影响。通过使用匿名对象或确保所有Where
过滤在AsEnumerable
之前完成,可能值得修剪您选择的列。Change it to:
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 anIEnumerable
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 yourWhere
filtering is done prior to theAsEnumerable
.