【问题标题】:Dynamic Linq Querystring not working with mixed operators along with contains动态 Linq 查询字符串不能与包含的混合运算符一起使用
【发布时间】:2017-01-09 07:52:55
【问题描述】:

我在我的应用程序中使用了Dynamic Linq,不幸的是它不起作用。 这是我的示例代码。

List<string> contains = new List<string>() { "Poland", "Ecuador" };
List<object> array = new List<object>();
array.Add("Austria");
array.Add("Eritrea");
array.Add(contains);

//this works
var data0 = Contact.GetContactsList().AsQueryable().Where("@0.Contains(outerIt.Country)", array);

//this does not work
var data = Contact.GetContactsList().AsQueryable().Where("Country.Equals(@0) OR it.Country.Equals(@1) OR @3.Contains(outerIt.Country)", array.ToArray());

我有与国家相关的联系人列表。 您可以看到我使用了混合运算符,即等于和包含。 如果 contains 单独使用,那么它是正确的。 我尝试在这里做新的事情,同时使用 Equals 和 Contains 运算符是相同的查询字符串。 我附上了描述输出或data0 的图像以及处理获取data 时出现的错误

谢谢。

【问题讨论】:

  • 当您将数组创建为 List&lt;Object&gt; 并将其添加到 contains 时,您最终会得到一个列表,其中前两个元素是字符串 "Austria" 、 "Eritrea" 和第三个元素本身就是一个包含 2 个字符串的列表。这看起来很奇怪,可能不是你想要做的。此外,您编写的查询没有引用您发送的数据样本,因此很难知道您的查询有什么问题。也许发送一个包含您查询的实际数据类型的样本?
  • 看起来像一个简单的错字 - @3 应该是 @2

标签: c# .net linq linq-to-sql dynamic-linq


【解决方案1】:

我在那里犯了一个愚蠢的错误。代码工作正常,但我在那里给出了错误的索引。索引 3 应替换为 2。:P enter image description here

结果附上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-04
    • 2018-08-07
    • 2021-11-21
    • 2011-10-24
    • 2014-03-05
    • 2017-10-25
    • 1970-01-01
    • 2017-11-19
    相关资源
    最近更新 更多