【问题标题】:Dynamic Linq core动态 Linq 核心
【发布时间】:2020-01-16 18:38:18
【问题描述】:

您好,我正在使用 Jqwidgets Grid 来显示我的数据。它具有使用过滤器的内置可能性,但是如果您在服务器端过滤记录,则必须构建自己的查询。当我使用 Linq 时,我想为 Asp 网络核心使用动态 Linq 库。问题是没有太多的例子或解释如何做到这一点。但是我现在忙了几天,并没有走得太远。我的设置方式;我有一个正常的 Linq 查询:

 var Mut = from M in _DB.Mutations
                  join S in _DB.Shifts on M.ShiftId equals S.ShiftId
                  join U in _DB.RoosterUsers on M.UserId equals U.RoosterUserId
                  join D in deps on M.UserId equals D.UserId
                  join DD in _DB.Departements on D.DepartementID equals DD.DepartementId
                  select new MutationModel
                  {
                      MutId=M.MutationId,
                      Naam=U.FirstName + " " + U.LastName,
                      UserId=M.UserId,
                      Departement= DD.DepartementName,
                      MutationType = S.publicName,
                      MutationGroup = S.ShiftType.ToString(),
                      DateTot =M.DateTill,
                      TijdVan=M.DateStartOn,
                      TijdTot=M.DateTill,
                      Status=CreateStatus(M.Tentative, M.ApprovedOn, M.Processed, M.CancelRefId, M.Deleted)
                  };

此查询运行正常,并为我提供了网格所需的所有数据。

然后对于过滤器,我想使用 System.Linq.Dynamic.Core 库添加动态 Linq 查询 但就我目前为止的工作而言:

var outQuery = Mut.Where("Status = @0 and UserId = @1", "Nieuw", "KLM22940").Select("Status");

我现在的问题: 1. 在 where 子句中,如果我将 fieldname 设为变量,则会出现错误。这该怎么做?? 2、在Select Clause中,如何添加多个Column? (其实我只是喜欢输出所有的列。)

  1. 最好是看一个例子。有人使用 Dynamic Linq 为 JQWidgets Grid 构建动态 linq 查询吗?

非常感谢。

【问题讨论】:

  • 您如何尝试使 fieldname 变量?展示你正在尝试什么。匿名对象使用"new(field1, field2 as name2, field3)" 创建。见here
  • 我做了 var outQuery = Mut.Where("@0 = @1","Status", "Nieuw") 但这给出了一个错误。
  • 你需要做var f = "Status"; outQuery = Mut.Where($"{f} = @1", "Nieuw") - 你不能为字段名使用参数。
  • 这 ($"{f} = @0", "Nieuw") 似乎有效。但我不明白你在这里做什么。你能解释一下这里的$是什么意思吗?
  • 是的,这是有效的:var f = "Status"; var outQuery = Mut.Where($"{f} = @0", "Nieuw").Select("new(MutId, Naam, UserId, Status)");明天我会继续这个。谢谢伊恩的帮助。

标签: linq asp.net-core dynamic-linq


【解决方案1】:

您尝试在 where 子句中以什么方式使用 fieldname 变量?

如果要输出所有列,可以使用 ToList() 喜欢

var outQuery = Mut.Where("Status = @0 and UserId = @1", "Nieuw", "KLM22940").ToList();

如果你想得到一些特定的列,你可以像这样使用 Select 子句

var outQuery = Mut.Where("Status = @0 and UserId = @1", "Nieuw", "KLM22940").Select("new(Status,UserId )");

此 Select 子句创建包含 Status 和 UserId 属性的数据类,并返回该数据类的一系列实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多