3

I need to count the number of the rows in my database with the entity framework. I am using the LINQ method "Count".

Here is the code:

QvDb dba = new QvDb();
if (dba.KUser.Count(us => us.FacebookId == values["FacebookId"]) == 0)

As you can see the values["FacebookId"] it's a post array variable, and the dba object variable it's the database model builder.

When I am trying to access to the page i got this exception:

An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method, and this method cannot be translated into a store expression.

For the record, the array is not null. It is a string that posted from the form.

4

1 回答 1

5

使用 LINQ to 实体时,数据库必须支持 LINQ 语句的所有部分。

values["FacebookId"]是本地字典。所以它不能在远程 SQL 数据库上执行。

首先将字典中的值提取到局部变量中,然后执行您的 LINQ 语句。

QvDb dba = new QvDb();
var id = values["FacebookId"];
if (dba.KUser.Count(us => us.FacebookId == id) == 0)
于 2013-06-29T18:31:31.800 回答