【问题标题】:System.Linq.Dynamic.Core cannot cast 'object' type to 'Tuple<T1, T2, T3>'System.Linq.Dynamic.Core 无法将 'object' 类型转换为 'Tuple<T1, T2, T3>'
【发布时间】:2021-03-21 06:41:16
【问题描述】:

我正在使用System.Linq.Dynamic.Core 并尝试使用以下模型运行动态查询:

public class Index
{
    public long Position { get; set; }

    public object[] Values { get; set; }
}

还有这样的数据样本:

var indexes = new Dictionary<long, Index>();
indexes.Add(1, new Index { Position = 1000, Values = new object[3] { "Welly", "Chandra", Tuple.Create<int, int>(1, 2) } });
indexes.Add(2, new Index { Position = 1001, Values = new object[3] { "Darma", "Angelo", Tuple.Create<int, int>(3, 4) } });
indexes.Add(3, new Index { Position = 1002, Values = new object[3] { "Abby", "Yeremia", Tuple.Create<int, int>(5, 6)} });
indexes.Add(4, new Index { Position = 1003, Values = new object[3] { "Yonathan", "Gunawan", Tuple.Create<int, int>(7, 8)} });
indexes.Add(5, new Index { Position = 1004, Values = new object[3] { "Aldy", "Santoso", Tuple.Create<int, int>(11, 12)} });
var queryable = indexes.Values.AsQueryable();
var result = queryable.Where("Values[1].Equals(\"Yeremia\") || ((Tuple<int, int>) Values[2]).Item2.Equals(8)").ToList();

但它总是抛出异常:

Exception: No property or field 'Tuple' exists in ...

这里查询时不能使用type-casting吗?

【问题讨论】:

    标签: c# linq dynamic-linq


    【解决方案1】:

    我不明白你为什么在 linq where 子句中写字符串。

    以下是我的完整工作代码。

    var indexes = new Dictionary<long, Index>();
                indexes.Add(1, new Index { Position = 1000, Values = new object[3] { "Welly", "Chandra", Tuple.Create<int, int>(1, 2) } });
                indexes.Add(2, new Index { Position = 1001, Values = new object[3] { "Darma", "Angelo", Tuple.Create<int, int>(3, 4) } });
                indexes.Add(3, new Index { Position = 1002, Values = new object[3] { "Abby", "Yeremia", Tuple.Create<int, int>(5, 6) } });
                indexes.Add(4, new Index { Position = 1003, Values = new object[3] { "Yonathan", "Gunawan", Tuple.Create<int, int>(7, 8) } });
                indexes.Add(5, new Index { Position = 1004, Values = new object[3] { "Aldy", "Santoso", Tuple.Create<int, int>(11, 12) } });
                var queryable = indexes.Values.AsQueryable();
                var result = queryable.Where(cc=>cc.Values[1].Equals("Yeremia") || ((Tuple<int, int>) cc.Values[2]).Item2.Equals(8)).ToList();
    

    更新:这对我有用。不需要类型转换。

     var indexes = new Dictionary<long, Index>();
                indexes.Add(1, new Index { Position = 1000, Values = new object[3] { "Welly", "Chandra", Tuple.Create<int, int>(1, 2) } });
                indexes.Add(2, new Index { Position = 1001, Values = new object[3] { "Darma", "Angelo", Tuple.Create<int, int>(3, 4) } });
                indexes.Add(3, new Index { Position = 1002, Values = new object[3] { "Abby", "Yeremia", Tuple.Create<int, int>(5, 6) } });
                indexes.Add(4, new Index { Position = 1003, Values = new object[3] { "Yonathan", "Gunawan", Tuple.Create<int, int>(7, 8) } });
                indexes.Add(5, new Index { Position = 1004, Values = new object[3] { "Aldy", "Santoso", Tuple.Create<int, int>(11, 12) } });
                var queryable = indexes.Values.AsQueryable();
                var result = queryable.Where("Values[1].Equals(\"Yeremia\") || Values[2].Item2.Equals(8)").ToList();
    

    【讨论】:

    • 因为我在后端做了一个动态的类似 SQL 的查询字符串生成器,所以我需要构造一个字符串作为 Where(...) 参数
    • 如果您使用的是 linq,那么您必须使用表达式来进行动态查询。此外,如果上面的代码在您的机器中编译,则意味着您正在使用您的代码中未提及的其他内容。
    • 是的,我使用github.com/zzzprojects/System.Linq.Dynamic.Core 库,正如问题标题中提到的System.Linq.Dynamic.Core
    • 这里抛出Exception: Target object is not an ExpandoObject
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    相关资源
    最近更新 更多