与原始ADO.NET相比,我遇到了Linq2Sql的性能问题,这导致了我编译查询的路径。我有这么远到目前为止如何编写动态Linq2Sql编译查询?
public static readonly Func<MyDataContext, WebServices.Search.Parameters, IQueryable<image>>
Compiled_SelectImagesLinq =
CompiledQuery.Compile<MyDataContext, WebServices.Search.Parameters, IQueryable<image>>(
(dc, parameters) => from i in dc.images
join l in dc.links on i.image_id equals l.image_id
join r in dc.resolutions on i.image_id equals r.image_id
where i.image_enabled == true && i.image_rating >= parameters.MinRating
&& i.image_rating <= parameters.MaxRating
select i
);
但我无法弄清楚如何将额外的可选参数添加到查询作为我目前做
if (parameters.Country != null)
{
query = query.Where(x => x.image_country_id == parameters.Country);
}
if (parameters.ComponentId != null)
{
query = query.Where(x => x.links.Any(l => l.link_component_id == parameters.ComponentId));
}
等,等
我尝试编写另一个功能
var query = Compiled_SelectImagesLinq(parameters);
然后将其他参数添加到查询中并返回
return query.Distinct().Take(parameters.Results);
位这似乎并不正确,并没有返回结果
查询将每天执行1000次。这些参数是作为从网页调用Web服务的结果生成的,以基于多个标准从云存储中检索相关图像。希望这是有道理的。我开始认为我试图强制Linq问题,应该只使用Raw ADO来提高性能和简化自由文本索引搜索 – 2009-06-19 15:57:59