CompiledQuery和Guid [英] CompiledQuery and Guid

查看:93
本文介绍了CompiledQuery和Guid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个listalbum,想要使用compiledquery,代码如下: -



public List< PhotoAlbum> ListAlbum(Guid blocId)
{
using(HomeBloc2Entities hb = new HomeBloc2Entities())
{
return CompiledListAlbum(hb,blocId);
}
}



private static Func< HomeBloc2Entities,Guid,List< PhotoAlbum>> CompiledListAlbum =
CompiledQuery.Compile((HomeBloc2Entities entities,Guid blocId)=>
(来自p的实体.PhotoAlbum,其中p.BlocId == blocId选择p).ToList< PhotoAlbum>());



但是当我调用它时,它有以下错误。



LINQ to Entities无法识别方法'System.Collections.Generic.List`1 [ACI.HomeBloc2.BLL.PhotoAlbum] ToList [PhotoAlbum](系统。 Collections.Generic.IEnumerable`1 [ACI.HomeBloc2.BLL.PhotoAlbum])'方法,此方法无法转换为商店表达式。



但是,如果我不使用编译的查询,它没有任何问题。



CompiledQuery支持guid吗?



问候


Alex

解决方案

发生这种情况,因为您在CompiledQuery中包含了对.ToList()的调用。 Linq to Entities不知道如何将其转换为适当的T-SQL,因此错误信息。

请尝试以下操作:


私有静态功能< HomeBloc2Entities,Guid,List< PhotoAlbum>> CompiledListAlbum =
CompiledQuery.Compile((HomeBloc2Entities entities,Guid blocId)=>
(来自p实体.PhotoAlbum,其中p.BlocId == blocId选择p) .ToList< PhotoAlbum>();


唯一的区别(突出显示)是现在在CompiledQuery之外调用ToList()。 />
你的查询在非CompiledQuery场景中工作的原因是因为ToList()没有被翻译成T-SQL。您的查询可能如下所示:

来自p中的entity.PhotoAlbum,其中p.BlocId == blocId选择p )。ToList();

highligted部分是转换为T-SQL,但ToList()不是。 ToList()转换在客户端上通过Linq到Objects进行。

如果你做了这样的事情:

.PhotoAlbum,其中p.SomeCollection.ToList()。Count()== 1选择p 。。ToList();

你会得到同样的错误正如您在CompiledQuery场景中所经历的那样(此时请注意突出显示区域中的ToList())。

希望这有帮助,
Maurycy



Hi All, I have a listalbum and want to use the compiledquery and the code is as follows:-

 

 public List<PhotoAlbum> ListAlbum(Guid blocId)
        {
            using (HomeBloc2Entities hb = new HomeBloc2Entities())
            {
                return CompiledListAlbum(hb, blocId);
            }
        }

 

  private static Func<HomeBloc2Entities, Guid, List<PhotoAlbum>> CompiledListAlbum=
        CompiledQuery.Compile((HomeBloc2Entities entities, Guid blocId) =>
        (from p in entities.PhotoAlbum where p.BlocId==blocId select p).ToList<PhotoAlbum>());

 

But when I call it, it has the following error.

 

LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[ACI.HomeBloc2.BLL.PhotoAlbum] ToList[PhotoAlbum](System.Collections.Generic.IEnumerable`1[ACI.HomeBloc2.BLL.PhotoAlbum])' method, and this method cannot be translated into a store expression.

 

However, it don't have any problem if I don't use the compiled query.

 

Is the guid supported under CompiledQuery?

 

Regards

Alex

解决方案

This happens, because you included the call to .ToList() inside the CompiledQuery. Linq to Entities does not know how to translate this into appropriate T-SQL and hence the error message.

Please try the following:


private static Func<HomeBloc2Entities, Guid, List<PhotoAlbum>> CompiledListAlbum=
        CompiledQuery.Compile((HomeBloc2Entities entities, Guid blocId) =>
        (from p in entities.PhotoAlbum where p.BlocId==blocId select p)).ToList<PhotoAlbum>();

The only difference (highlighted) is that now the ToList() is called outside the CompiledQuery.


The reason why your query worked in non-CompiledQuery scenario is because the ToList() was not translated into T-SQL. Your query probably looked like this:

(from p in entities.PhotoAlbum where p.BlocId == blocId select p).ToList();

The highligted part is translated into the T-SQL, however the ToList() is not. The ToList() translation is happening on the client via Linq to Objects.


If you did something like this:

(from p in entities.PhotoAlbum where p.SomeCollection.ToList().Count() == 1 select p).ToList();

you would get the same error as you experienced in the CompiledQuery scenario (notice the ToList() in the highlighted area this time).


Hope this helps,
Maurycy



这篇关于CompiledQuery和Guid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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