Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# 使用LINQ和实体框架启动从列表到SQL请求的任意字符串_C#_Postgresql_Linq_Entity Framework Core - Fatal编程技术网

C# 使用LINQ和实体框架启动从列表到SQL请求的任意字符串

C# 使用LINQ和实体框架启动从列表到SQL请求的任意字符串,c#,postgresql,linq,entity-framework-core,C#,Postgresql,Linq,Entity Framework Core,我可以通过以下请求从EF获取对象: apples = apples.Where(a => a.Sort.Name.StartsWith("Gold")) 但我想知道是否有可能使用字符串列表而不是一个字符串? 我已经试过这样做: List<string> list = {...} apples = apples.Where(a => list.Any(x => a.Sort.Name.StartsWith(x))) List List={…}

我可以通过以下请求从EF获取对象:

apples = apples.Where(a => a.Sort.Name.StartsWith("Gold"))
但我想知道是否有可能使用字符串列表而不是一个字符串? 我已经试过这样做:

List<string> list = {...}

apples = apples.Where(a => list.Any(x => a.Sort.Name.StartsWith(x)))
List List={…}
apples=apples.Where(a=>list.Any(x=>a.Sort.Name.StartsWith(x)))
但它给了我一个奇怪的错误:

System.ArgumentOutOfRangeException:指定的参数超出有效值的范围。(参数“索引”) 位于System.Linq.Expressions.InstanceMethodCallExpression1.GetArgument(Int32索引) 位于Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionVisitors.NpgsqlSqlTranslatingExpressionVisitor.VisitLikeAnyAll(子查询表达式) 位于Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionVisitors.NpgsqlSqlTranslatingExpressionVisitor.VisitSubQueryExpression查询 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visite(表达式) 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitBinary(二进制表达式) 位于Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionVisitors.npgsqlTranslatingExpressionVisitor.VisitBinary(二进制表达式) 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visite(表达式) 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitBinary(二进制表达式) 位于Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionVisitors.npgsqlTranslatingExpressionVisitor.VisitBinary(二进制表达式) 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visite(表达式) 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitBinary(二进制表达式) 位于Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionVisitors.npgsqlTranslatingExpressionVisitor.VisitBinary(二进制表达式) 位于Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.Visite(表达式) 在Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitWhere子句(Where子句Where子句、QueryModel QueryModel、Int32索引) 在Remotion.Linq.QueryModelVisitorBase.VisitByClauses(ObservableCollection
1 bodyClauses,QueryModel QueryModel)在Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel QueryModel QueryModel)在Microsoft.EntityFrameworkCore.Query.EntityQueryModel.VisityModel(QueryModel QueryModel QueryModel)上在Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel QueryModel)在Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor[TResult](QueryModel QueryModel)在Microsoft.EntityFrameworkCore.Storage.Database.CompileAsyncQuery[TResult](QueryModel QueryModel)在Microsoft.EntityFrameworkCore.Storage.Database在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileAsyncQueryCore[TResult](表达式查询、IQueryModelGenerator查询模型生成器、IDatabase数据库)的Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.c_u显示类22_0
1.b_u0() 在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiledQueryCache.GetOrAddQueryCore[TFunc](对象缓存键,Func
1编译器)在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](表达式查询)中位于Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable
1.System.Collections.Generic.IAsyncEnumerable.GetEnumerator() 在D:\a\1\s\Ix.NET\source\System.Interactive.Async\Aggregate.cs:第118行{my project}中的System.Linq.AsyncEnumerable.Aggregate_u0;[TSource,tacumulate,TResult](IAsyncenuerable
1 source,tacumulate seed,Func
3 accumulator,Func`2 resultSelector,CancellationToken CancellationToken CancellationToken)中


无法得到与您相同的错误(得到的只是一个翻译失败的错误,因此如果您可以添加,那将非常好),因为我使用的是
EF.Functions.ILike
(使用最新的npgsql软件包):

List List=new();
var结果=ctx.Apples
.Where(c=>list.Any(xx=>EF.Functions.ILike(c.Sort.Name,xx)))
.ToList();
据我所知,目前还没有实现对本地集合使用
StartsWith
的支持(基于和),只有
EF.Functions.Like
EF.Functions.ILike

如果您愿意使用,那么您可以创建扩展方法来从本地集合构建表达式:

// searchTerms - IEnumerable<TSearch> where one must match for a row
// testFne(row,searchTerm) - test one of searchTerms against a row
// r => searchTerms.Any(s => testFne(r,s))
public static Expression<Func<T, bool>> AnyIs<T, TSearch>(this IEnumerable<TSearch> searchTerms, Expression<Func<T, TSearch, bool>> testFne) {
    var pred = PredicateBuilder.New<T>();
    foreach (var s in searchTerms)
        pred = pred.Or(r => testFne.Invoke(r, s));

    return pred;
}

// searchTerms - IEnumerable<TSearch> where one must match for a row
// testFne(row,searchTerm) - test one of searchTerms against a row
// dbq.Where(r => searchTerms.Any(s => testFne(r,s)))
public static IQueryable<T> WhereAny<T,TSearch>(this IQueryable<T> dbq, IEnumerable<TSearch> searchTerms, Expression<Func<T, TSearch, bool>> testFne) =>
    dbq.AsExpandable().Where(searchTerms.AnyIs(testFne));
//searchTerms-IEnumerable,其中必须匹配一行
//testFne(row,searchTerm)-针对一行测试其中一个searchTerm
//r=>searchTerms.Any(s=>testFne(r,s))
公共静态表达式AnyIs(此IEnumerable searchTerms,表达式testFne){
var pred=PredicateBuilder.New();
foreach(搜索术语中的var s)
pred=pred.Or(r=>testFne.Invoke(r,s));
返回pred;
}
//searchTerms-IEnumerable,其中必须匹配一行
//testFne(row,searchTerm)-针对一行测试其中一个searchTerm
//其中(r=>searchTerms.Any(s=>testFne(r,s)))
公共静态IQueryable where(此IQueryable dbq、IEnumerable searchTerms、表达式testFne)=>
dbq.AsExpandable().Where(searchTerms.AnyIs(testFne));
然后,您可以将它们用于查询:

List<string> list = {...}

apples = apples.WhereAny(list, (a,s) => a.Sort.Name.StartsWith(s));
List List={…}
apples=apples.whereny(list,(a,s)=>a.Sort.Name.StartsWith(s));

当然,您也可以创建自己的mini-LINQKit来完成相同的任务(尽管它不是那么迷你)。

使用.Sort是什么意思?你在对一个苹果进行排序吗?排序是一个对象,有一个包含所有排序的表,所以每个苹果都有一个排序,每个排序都有一个名称。我只需要从数据库中选择哪些排序名称从列表中的任何字符串开始。您需要指定要使用的LINQ到EF:EF 6.x,EF Core 2.0/2.1/3.x/5.x?
List<string> list = {...}

apples = apples.WhereAny(list, (a,s) => a.Sort.Name.StartsWith(s));