Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么可以';我不能用include吗?_C#_.net_Linq_Lambda_Linq To Entities - Fatal编程技术网

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);
    }
}