LINQ到实体无法识别方法'System.String get_Item(System.String)“, [英] LINQ to Entities does not recognize the method 'System.String get_Item (System.String)',
问题描述
我该如何解决这个问题呢?
How can I solve this problem?
下面是我的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到实体无法识别方法'System.String get_Item(System.String),它不能转换到存储库的前pression。
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查询将被转换为SQL它们可以被执行之前;但集[txtDtInicial]
不能转换为SQL,因为没有相应的SQL语法,反正数据库不能访问到集
。你需要提取集[txtDtInicial]
给一个变量第一,并在查询中只使用这个变量。
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.
下面是我会做:
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);
通过初始化 dtInicio
和 dtFim
来MINVALUE和MaxValue的,你并不需要检查它们是否被定义在查询中。
By initializing dtInicio
and dtFim
to MinValue and MaxValue, you don't need to check whether they are defined in the query.
这篇关于LINQ到实体无法识别方法'System.String get_Item(System.String)“,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!