在 Linq 中将字符串作为查询过滤器发送到对象

时间:2021-05-19 12:14:21

标签: c# linq xamarin xamarin.forms mobile

我在移动项目(.net core 3.0)中使用 sqllite,就像我在使用 EF core(.Net 5)的 web 项目中所做的那样,我写了这个:

InsAndOuts = await context._database.Table<Data.InOut>().ToListAsync();
var InsAndOutsQ = InsAndOuts.AsQueryable();

if (filter)
{ 
    InsAndOutsQ = InsAndOutsQ.Where(args.Filter); //<= This line is the problem

    InsAndOutsQ = InsAndOutsQ.Where(td => td.IsIn == isIn || td.IsIn == !isOut)
        .Where(td => td.Date >= model.StartDate && td.Date <= model.EndDate);
}

if (sort)
{
    InsAndOutsQ = InsAndOutsQ.OrderBy(args.OrderBy); //<= this line also get error
}

我这里不需要效率,因为列表不多,错误是:

    Severity    Code    Description Project File    Line    Suppression State
Error   CS1503  Argument 2: cannot convert from 'string' to 'System.Func<MobileBlazorHybrid.Data.InOut, bool>'  MobileBlazorHybrid  C:\...\MobileBlazorHybrid\MobileBlazorHybrid\WebUI\Pages\InOut.razor    343 Active

    Severity    Code    Description Project File    Line    Suppression State
Error   CS0411  The type arguments for method 'Enumerable.OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.   MobileBlazorHybrid  C:\...\MobileBlazorHybrid\MobileBlazorHybrid\WebUI\Pages\InOut.razor    351 Active

过滤器和 orderby 是由数据网格动态构建的,它在具有 Ef 核心 5 的 .Net 5 中工作,但在这里我有 sqllite 并且没有 EF 核心,而 .net 是 3.0。 如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我添加了包 Microsoft.EntityFrameworkCore.Sqlite 并使用 System.Linq.Dynamic.Core 解决了我的问题。