C# 为什么可以';我不能用include吗?
这是我的密码:C# 为什么可以';我不能用include吗?,c#,.net,linq,lambda,linq-to-entities,C#,.net,Linq,Lambda,Linq To Entities,这是我的密码: ProjetoTipoCargaModelo projAux = dbContext.ProjetoTipoCargaDbSet.Find(idProjetoTipoCarga); ICollection<ProjetoTipoCargaRegraModelo> regras = projAux.ListaRegra.Where(x => x.Ativo).ToList(); IQueryable<ProjetoTipoCargaRegra
ProjetoTipoCargaModelo projAux =
dbContext.ProjetoTipoCargaDbSet.Find(idProjetoTipoCarga);
ICollection<ProjetoTipoCargaRegraModelo> regras =
projAux.ListaRegra.Where(x => x.Ativo).ToList();
IQueryable<ProjetoTipoCargaRegraModelo> pr =
dbContext.ProjetoTipoCargaDbSet.Select(
x => regras.FirstOrDefault(y => y.IdProjetoTipoCarga == x.IdProjetoTipoCarga));
var projetoCompleto = pr.
Include(x => x.ListaRegraLiberacaoInicioViagem).
Include(x => x.ListaRegraTecnologiaAceita).
Include(x => x.RegraAreaSombra).
Include(x => x.RegraAtuadorNecessario)
[编辑3]
答复:
ICollection reglas=projAux.ListaRegra.Where(x=>x.Ativo.ToList();
IQueryable pr=dbContext.ProjetoTipoCargaDbSet.Where(x=>x.IdProjetoTipoCarga==reglas.FirstOrDefault(y=>y.IdProjetoTipoCarga==x.IdProjetoTipoCarga).IdProjetoTipoCarga);
var projetoCompleto=pr。
包括(x=>x.listaregraliberacaoinicioviaegem)。
包括(x=>x.ListaRegraTecnologiaAceita)。
包括(x=>x.RegraAreaSombra)。
它与包含的无关
不能在LINQ to entities查询中使用regas
,因为它是ICollection
。EF无法将其转换为SQL
您的“答案”不可能与EF一起使用。对象regas
是内存中的Regra
实体列表(我猜)。如果你直接用在
var pr = dbContext.ProjetoTipoCargaDbSet
.Where(x => x.IdProjetoTipoCarga == regras.FirstOrDefault(y => y.IdProjetoTipoCarga == x.IdProjetoTipoCarga).IdProjetoTipoCarga);
…你应该得到这样一个例外
无法创建“Regra”类型的常量值。仅支持基元类型或枚举类型
但是,孩子,要想到达你想去的地方,这是一条多么曲折的道路啊!首先,通过idProjetoTipoCarga
获得一个ProjetoTipoCargaModelo
对象。然后获取其活动的Regra
s。然后基本上使用Regra
s的IdProjetoTipoCarga
值来查看其中一个是否等于原始IdProjetoTipoCarga
,如果是,则使用其值来获取ProjetoTipoCargaModelo
对象
如果删除所有冗余,剩下的是:
var pr = dbContext.ProjetoTipoCargaDbSet
.Where(x => x.IdProjetoTipoCarga == idProjetoTipoCarga
&& x.ListaRegra.Any(r => r.Ativo));
如果使用此LINQ语句,则将Include附加到pr
中。Include()方法仅适用于ObjectQuery
尝试:
而不是:
context.EntitySet.Select(...).Include(...)
或者使用如下扩展方法:
public static class MyExtensions
{
public static IQueryable<TEntity> Include<TEntity>(
this IQueryable<TEntity> query, string path)
{
var efQuery = query as ObjectQuery<TEntity>;
if (efQuery == null)
return query;
return efQuery.Include(path);
}
}
公共静态类MyExtensions
{
公共静态索引包括(
此IQueryable查询,字符串路径)
{
var efQuery=查询为ObjectQuery;
if(efQuery==null)
返回查询;
返回efQuery.Include(路径);
}
}
或者更好的是,使用已经可用的扩展方法,该方法支持lambda表达式而不是字符串作为路径
另外,不要使用太多的include,除非大多数是1:1或1:1关系,1:(或:)关系会大大增加来自数据库的IO,从而导致性能不佳
考虑将多个查询与.Future()一起使用,以启用对数据库的单一访问。如果您自己能够找到答案,请将其作为您自己问题的答案发布(而不是将其放在问题的编辑中)。不过,看起来你的答案并不是真正的答案。当然,你已经解决了一个问题,但是你解决的问题看起来不可能导致你所犯的错误。投票结果是什么?这个答案确实有效。我已经多次使用了所有这些选项。@hvd我知道,我的答案确实表明扩展方法已经存在,我只是简单地演示了如何使用。@hvd将答案读到最后,它解释了它为什么有效,使其工作的所有备选方案,以及为什么不应使用它们,以及如何取而代之。@hvg注意我回答的第一部分-更改操作顺序可以在不使用任何扩展方法的情况下使用.Include()。我同意我以前的评论没有抓住要点。在这个问题中,OP试图调用IQueryable
上的Include
,但是,我无法从您的答案中了解为什么它不能编译,以及为什么您的更改可以解决它。
context.EntitySet.Include(...).Select(...)
context.EntitySet.Select(...).Include(...)
public static class MyExtensions
{
public static IQueryable<TEntity> Include<TEntity>(
this IQueryable<TEntity> query, string path)
{
var efQuery = query as ObjectQuery<TEntity>;
if (efQuery == null)
return query;
return efQuery.Include(path);
}
}