简体   繁体   English

实体框架错误,试图获取Linq到实体的行数

[英]Entity Framework Error Trying to Get Row Count in Linq to Entities

When I try to run this code: 当我尝试运行此代码时:

var qry = ( from v in db.Visits
           where v.VisitorID == visitorID
              && v.IncomingID == incomingID
              && v.ProjectID == ProjectID.GetID()
           select v.VisitorID);

int visits = qry.Count();

The qry runs okay but the "int visits = ..." line bombs out with "LINQ to Entities does not recognize the method 'Int32 GetID()' method, and this method cannot be translated into a store expression." qry运行正常,但是用“ LINQ to Entities无法识别'Int32 GetID()'方法,并且该方法无法转换为商店表达式”的“ int visits = ...”行炸了。

What am I doing wrong? 我究竟做错了什么?

what you are doing wrong, is that ProjectID.GetID() can not be called in linq to entity, you can call this before your query: 您做错的是,不能在linq中调用 ProjectID.GetID()实体,您可以在查询之前调用此函数:

var o = ProjectID.GetID();
var qry = (from v in db.Visits
           where v.VisitorID == visitorID && 
                 v.IncomingID == incomingID && 
                 v.ProjectID == o 
           select v.VisitorID);

int visits = qry.Count();

You can't call custom methods in a query like this. 您不能在这样的查询中调用自定义方法。

LINQ to Entities works by building an expression tree in order to convert to a database query. LINQ to Entities通过构建表达式树来转换为数据库查询。 It sees ProjectID.GetID() and it has no idea how to translate that into SQL. 它看到ProjectID.GetID(),并且不知道如何将其转换为SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM