Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 在SelectMany LINQ to Entity框架中使用PredicateBuilder_C#_Linq_Entity Framework 6_Predicatebuilder_Linqkit - Fatal编程技术网

C# 在SelectMany LINQ to Entity框架中使用PredicateBuilder

C# 在SelectMany LINQ to Entity框架中使用PredicateBuilder,c#,linq,entity-framework-6,predicatebuilder,linqkit,C#,Linq,Entity Framework 6,Predicatebuilder,Linqkit,这是我第一次在stackoverflow上写这篇文章 我想在带有SelectMany的LINQ查询中使用PredicateBuilder 我输入我的代码 public async Task<List<Articolo>> OttieniElencoArticoliFiltratoComplessoAsync (ExpressionStarter<Articolo> predicateArticolo, ExpressionStarter

这是我第一次在stackoverflow上写这篇文章

我想在带有SelectMany的LINQ查询中使用PredicateBuilder

我输入我的代码

public async Task<List<Articolo>> OttieniElencoArticoliFiltratoComplessoAsync
    (ExpressionStarter<Articolo> predicateArticolo,
        ExpressionStarter<ArticoloFornitore> predicateFornitore,
        ExpressionStarter<Categoria> predicateCategoria,
        ExpressionStarter<Magazzino> predicateMagazzino,
        ExpressionStarter<PrezzoVendita> predicatePrezzoVendita)
    {
        using (var database = DatabaseContext.NuovoDatabase())
        {
            var query = database.Articoli.AsExpandable();

            if (predicateArticolo != null)
            {
                query = query.Where(predicateArticolo);
            }

            if (predicateFornitore != null)
            {
                query = query.AsExpandable().SelectMany(a => a.ElencoArticoliFornitore.Where(predicateFornitore)).Select(fornitore => fornitore.Articolo);
            }

            if (predicateMagazzino != null)
            {
                query = query.AsExpandable()
                    .SelectMany(articolo => articolo.ElencoMagazzino.Where(predicateMagazzino))
                    .Select(magazzino => magazzino.Articolo);
            }

            if (predicatePrezzoVendita != null)
            {
                query = query.AsExpandable().SelectMany(articolo =>
                    articolo.ElencoPrezzoVendita.Where(predicatePrezzoVendita).Select(vendita => vendita.Articolo));
            }

            if (predicateCategoria != null)
            {
                query = query.AsExpandable()
                    .SelectMany(articolo => articolo.ElencoCategorie.Where(predicateCategoria))
                    .Select(categoria => categoria.Articolo);
            }

            return await query.ToListAsync();
        }
    }
public异步任务ottienielencoarticolifilternocomplessoancy
(ExpressionStarter predicateArticolo,
ExpressionStarter谓词编辑器,
表达式开始谓词类别,
ExpressionStarter PredicateMagazino,
ExpressionStarter谓词prezepzovendita)
{
使用(var database=DatabaseContext.NuovoDatabase())
{
var query=database.Articoli.AsExpandable();
if(predicateArticolo!=null)
{
query=query.Where(predicateArticolo);
}
if(谓词编辑器!=null)
{
query=query.AsExpandable();
}
if(predicateMagazino!=null)
{
query=query.AsExpandable()
.SelectMany(articolo=>articolo.ElencoMagazzino.Where(predicateMagazino))
.选择(magazzino=>magazzino.Articolo);
}
if(predicatePrezzovendta!=null)
{
query=query.AsExpandable().SelectMany(articolo=>
选择(vendita=>vendita.articolo));
}
if(谓词类别!=null)
{
query=query.AsExpandable()
.SelectMany(articolo=>articolo.elencocategie.Where(谓词范畴))
.选择(categoria=>categoria.Articolo);
}
return wait query.ToListAsync();
}
}
我这样创建谓词

        private ExpressionStarter<ArticoloFornitore> PredicateFornitore()
    {
        var ritornaNull = true;
        var predicate = PredicateBuilder.New<ArticoloFornitore>();
        if (IsEnabledFiltroFornitore && FornitoreSelezionato != null)
        {
            ritornaNull = false;
            predicate = predicate.And(fornitore => fornitore.IdFornitore == FornitoreSelezionato.IdFornitore);
        }
        return ritornaNull ? null : predicate;
    }

    private ExpressionStarter<Categoria> PredicateCategoria()
    {
        var ritornaNull = true;
        var predicate = PredicateBuilder.New<Categoria>();
        if (IsEnabledCategoriaLivello1 && CategoriaLivello1Selezionata != null)
        {
            ritornaNull = false;
            predicate = predicate.And(categoria =>
                categoria.IdCategoriaLv1 == CategoriaLivello1Selezionata.IdCategoriaLv1);
        }

        if (IsEnabledCategoriaLivello2 && CategoriaLivello2Selezionata != null)
        {
            ritornaNull = false;
            predicate = predicate.And(categoria =>
                categoria.IdCategoriaLv2 == CategoriaLivello2Selezionata.IdCategoriaLv2);
        }

        if (IsEnabledCategoriaLivello3 && CategoriaLivello3Selezionata != null)
        {
            ritornaNull = false;
            predicate = predicate.And(categoria =>
                categoria.IdCategoriaLv3 == CategoriaLivello3Selezionata.IdCategoriaLv3);
        }

        if (IsEnabledCategoriaLivello4 && CategoriaLivello4Selezionata != null)
        {
            ritornaNull = false;
            predicate = predicate.And(categoria =>
                categoria.IdCategoriaLv4 == CategoriaLivello4Selezionata.IdCategoriaLv4);
        }

        if (IsEnabledCategoriaLivello5 && CategoriaLivello5Selezionata != null)
        {
            ritornaNull = false;
            predicate = predicate.And(categoria =>
                categoria.IdCategoriaLv5 == CategoriaLivello5Selezionata.IdCategoriaLv5);
        }
        return ritornaNull ? null : predicate;
    }
private ExpressionStarter谓词编辑器()
{
var ritornaNull=真;
var predicate=PredicateBuilder.New();
if(IsEnabledFiltroFornitore&&FornitoreSelezionato!=null)
{
ritornaNull=假;
谓词=谓词.And(fornitore=>fornitore.IdFornitore==FornitoreSelezionato.IdFornitore);
}
return ritornaNull?null:谓词;
}
private ExpressionStarter谓词类别()
{
var ritornaNull=真;
var predicate=PredicateBuilder.New();
if(IsEnabledCategalivelLo1&&CategalivelLo1Selezionata!=null)
{
ritornaNull=假;
谓词=谓词。和(范畴=>
categoria.IdCategoriaLv1==CategoriaLivello1Selezionata.IdCategoriaLv1);
}
if(IsEnabledCategoriaLivello2&&CategoriaLivello2Selezionata!=null)
{
ritornaNull=假;
谓词=谓词。和(范畴=>
categoria.IdCategoriaLv2==CategoriaLivello2Selezionata.IdCategoriaLv2);
}
if(IsEnabledCategalivelLo3&&CategalivelLo3Selizionata!=null)
{
ritornaNull=假;
谓词=谓词。和(范畴=>
categoria.IdCategoriaLv3==CategoriaLivello3Selezionata.IdCategoriaLv3);
}
if(IsEnabledCategalivelLo4&&CategalivelLo4Selezionata!=null)
{
ritornaNull=假;
谓词=谓词。和(范畴=>
categoria.IdCategoriaLv4==CategoriaLivello4Selezionata.IdCategoriaLv4);
}
if(IsEnabledCategalivelLo5&&CategalivelLo5Selezionata!=null)
{
ritornaNull=假;
谓词=谓词。和(范畴=>
categoria.IdCategoriaLv5==CategoriaLivello5Selezionata.IdCategoriaLv5);
}
return ritornaNull?null:谓词;
}
我在LINQPAD上使用固定数据而不是PredicateBuilder尝试了这种方法,查询工作正常。 但是使用PredicateBuilder,我得到了.NETFramework数据提供程序错误1025

我怎样才能解决这个问题?我希望能够创建一个动态查询,从接口获取参数并返回结果


希望您能帮助我。

EntityFramework要求您的谓词是
表达式
,而不是
函数
。这就是
IQueryable.ToExpandable()
所做的。但是,您的实体对象,例如
articolo.elencoprezzovedita
是虚拟的
ICollection
对象,因此谓词被简化为
Func
,这与EF不兼容

它并不完全是从舌头上滚下来的,但是你可以用数据库试试这样的东西(我不知道你的对象的结构),以确保使用
表达式

if (predicatePrezzoVendita != null)
{
    query = query.AsExpandable()
        .SelectMany(articolo =>
            database.ElencoPrezzoVendita
                .Where(x => articolo.ForeignKeyID == x.ID)
                .AsExpandable()
                .Where(predicatePrezzoVendita)
                .Select(vendita => vendita.Articolo));
}

您忘了包含使用PredicateBuilder的代码。我修改了帖子并添加了创建predicateSee的代码。我建议使用
Expression
而不是
ExpressionStarter
非常感谢,我用这种方法解决了我的问题。