C# LINQ to Entities 无法识别方法“System.String get_Item (System.String)”,

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10156659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:40:54  来源:igfitidea点击:

LINQ to Entities does not recognize the method 'System.String get_Item (System.String)',

c#asp.net-mvclinq-to-entities

提问by mcamara

How can I solve this problem?

我怎么解决这个问题?

Here is my code:

这是我的代码:

    DateTime dtInicio = new DateTime();
    DateTime dtFim = new DateTime();
    Int32 codStatus = 0;

    if(!string.IsNullOrEmpty(collection["txtDtInicial"]))
        dtInicio = Convert.ToDateTime(collection["txtDtInicial"]);
    if(!string.IsNullOrEmpty(collection["txtDtFinal"]))
        dtFim = Convert.ToDateTime(collection["txtDtFinal"]);
    if (!string.IsNullOrEmpty(collection["StatusCliente"]))
        Convert.ToInt32(collection["StatusCliente"]);

    var listCLientResult = (from c in db.tbClientes
                           orderby c.id
                            where (c.effdt >= dtInicio || string.IsNullOrEmpty(collection["txtDtInicial"]) &&
                                 (c.effdt <= dtFim || string.IsNullOrEmpty(collection["txtDtFinal"])) &&
                                 (c.cod_status_viagem == codStatus || string.IsNullOrEmpty(collection["StatusCliente"])))
                                 select c);
    return View(listCLientResult);

The error I am getting is:

我得到的错误是:

LINQ to Entities does not recognize the method 'System.String get_Item (System.String)', which can not be converted into an expression of the repository.

LINQ to Entities 无法识别方法“System.String get_Item (System.String)”,该方法无法转换为存储库的表达式。

采纳答案by Thomas Levesque

Linq queries performed against a database are translated to SQL before they can be executed; but collection["txtDtInicial"]can't be translated to SQL, because there is no equivalent SQL syntax, and anyway the database doesn't have access to collection. You need to extract collection["txtDtInicial"]to a variable first, and use only this variable in the query.

对数据库执行的 Linq 查询在执行之前被转换为 SQL;但collection["txtDtInicial"]无法转换为 SQL,因为没有等效的 SQL 语法,而且无论如何数据库无法访问collection. 您需要先提取collection["txtDtInicial"]到一个变量,并在查询中仅使用该变量。

Here's what I would do:

这是我会做的:

DateTime dtInicio = DateTime.MinValue;
DateTime dtFim = DateTime.MaxValue;
Int32 codStatus = 0;

if(!string.IsNullOrEmpty(collection["txtDtInicial"]))
    dtInicio = Convert.ToDateTime(collection["txtDtInicial"]);
if(!string.IsNullOrEmpty(collection["txtDtFinal"]))
    dtFim = Convert.ToDateTime(collection["txtDtFinal"]);
if (!string.IsNullOrEmpty(collection["StatusCliente"]))
    codStatus = Convert.ToInt32(collection["StatusCliente"]);

var listCLientResult = (from c in db.tbClientes
                       orderby c.id
                        where (c.effdt >= dtInicio) &&
                             (c.effdt <= dtFim) &&
                             (c.cod_status_viagem == codStatus)
                             select c);
return View(listCLientResult);

By initializing dtInicioand dtFimto MinValue and MaxValue, you don't need to check whether they are defined in the query.

通过将dtInicioand初始化dtFim为 MinValue 和 MaxValue,您无需检查它们是否在查询中定义。

回答by ravi saini

The Linq query is ultimately transformed into an SQL query and LINQ doesn't know what to do with Session["UserName"] (that gets the "UserName" item).

Linq 查询最终转换为 SQL 查询,而 LINQ 不知道如何处理 Session["UserName"](获取“UserName”项)。

A common way to workaround this is just to use a local variable to which you'll assign Session["UserName"] and that you'll use in your Linq query...

解决此问题的常用方法是使用局部变量,您将为其分配 Session["UserName"] 并且您将在 Linq 查询中使用该变量...

like

喜欢

string loggedUserName = Session["LogedUsername"].ToString();
var userdetail = dc.faculties.Where(a => a.F_UserName.Equals(loggedUserName)).FirstOrDefault();

string loggingUserName = Session["LogedUsername"].ToString();
var userdetail = dc.faculties.Where(a => a.F_UserName.Equals(loggedUserName)).FirstOrDefault();

reference http://mvc4asp.blogspot.in/

参考http://mvc4asp.blogspot.in/

回答by Amit Kadam

In one line...

在一行...

DO NOT USE STRING CONVERSION FUNCTIONS in Linq(Entity) Query!

不要在 Linq(Entity) 查询中使用字符串转换函数!

Wrong:

错误的:

user = db.Users.Where(u => u.Name == dt.Rows[i]["Name"].ToString()).FirstOrDefault();

Correct:

正确的:

string Name = dt.Rows[i]["Name"].ToString();
user = db.Users.Where(u => u.Name == Name).FirstOrDefault();