错误:LINQ to Entities不支持LINQ表达式节点类型'ArrayIndex' [英] Error: The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities

查看:229
本文介绍了错误:LINQ to Entities不支持LINQ表达式节点类型'ArrayIndex'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用MVC5应用.我有一个定义为...的字符串数组.

Working on MVC5 app. I have a string array defined as....

string[] myArray;

其中有3个项目....

It has 3 items in it....

{string[3]}
   [0]: "a411d1bc-21f7-4e4d-a4d3-4dd36e1b319f"
   [1]: "ef4e3655-fa6f-4dfc-b2d4-178ac5914f0b"
   [2]: "d75a98c5-a829-43c5-b2cf-d50be1189a05"

这些实际上是AspNetUser表中记录的主键.我想使用多个"WHERE"中的数组项执行EF WHERE子句. (因此,基本上我想从AspNetUser表中检索这3个用户.)到目前为止,这是我的代码...

These are actually the primary keys for records in the AspNetUser table. I want to execute an EF WHERE clause using the array items in multiple "WHERE". (So basically I want to retrieve these 3 users from the AspNetUser table.) Here's my code so far...

IQueryable<Event> events = db.Events;

if (myArray != null)
{
    events = events.Include(a => a.AspNetUser);

    for(int i = 0; i <= myArray.Length-1; i++)
    {
        events = events.Where(u => u.AspNetUser.Id == myArray[i].ToString());
    }
}

因此,如您所见,我基本上是遍历数组(包含所有PK),并在WHERE子句中使用它.

So, as you can see, I'm basically looping thru the array (that contains all the PK) and using it in my WHERE clause.

但是,当我在循环中点击第一个.WHERE行时,我得到了这个错误:

But, right when I hit the first .WHERE line in the loop I'm getting this error:

LINQ to Entities不支持LINQ表达式节点类型'ArrayIndex'.

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities.

我在这里做错了什么?有没有更好的方法?

What am I doing wrong here? Is there a better approach?

最后,对于我所描述的动态场景,如何在哪里使用OR语句而不是AND命令?

Finally, for a dynamic scenario such as I described, how can I used OR statements rather than an AND commands for the where?

推荐答案

在正常情况下,当在EntityFramework中执行类似Where的操作时,它实际上并没有像对可枚举的操作(例如一个列表).而是将其转换为SQL,然后由Db提供程序执行.这意味着不能执行某些操作,例如对对象使用扩展方法,或者在情况下按索引从数组中获取元素,因为转换器无法将这些操作转换为SQL.

Under normal circumstances, when performing an operation like a Where in EntityFramework, it isn't actually executed in memory like when you operate on an enumerable (like a List). Instead, it converted into SQL which is then executed by the Db provider. This means that doing certain things, such as using extension methods on objects or, in your case, getting elements from arrays by index is not an option, as the converter cannot turn such things into SQL.

要修复您的现有代码,您需要像这样修改for循环:

To fix your existing code, you need to modify your for loop like so:

for(int i = 0; i <= myArray.Length-1; i++)
{
    var temp = myArray[i].ToString();
    events = events.Where(u => u.AspNetUser.Id == temp);
}

将数组操作移到查询之外可以使EntityFramework正确地将其转换为SQL.

Moving the array operation outside of the query allows EntityFramework to convert it into SQL properly.

一种更好的方法可以完成以下任务:

A better approach to doing what you want would be the following:

var result = events.Where(u => myArray.Contains(u.AspNetUser.Id));

这篇关于错误:LINQ to Entities不支持LINQ表达式节点类型'ArrayIndex'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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