2012-01-12 30 views
7

我试图使用动态linq查询从对象集合(Linq到Object)检索IEnumerable <T>,每个对象都在该集合具有一个内部集合,其中包含另一组存储数据的对象,这些值可通过外部集合中的索引器访问Dynamic Linq - 对包含“dynamic”类型的成员执行查询

当您使用强类型对象时,动态linq查询将按预期返回过滤集但我的对象将数据存储在动态类型的成员中,请参阅下面的示例:

public class Data 
{ 
    public Data(string name, dynamic value) 
    { 
     this.Name = name; 
     this.Value = value; 
    } 

    public string Name { get; set; } 
    public dynamic Value { get; set; } 
} 

public class DataItem : IEnumerable 
{ 
    private List<Data> _collection; 

    public DataItem() 
    { _collection = new List<Data>(); } 

    public dynamic this[string name] 
    { 
     get 
     { 
      Data d; 
      if ((d = _collection.FirstOrDefault(i => i.Name == name)) == null) 
       return (null); 

      return (d.Value); 
     } 
    } 

    public void Add(Data data) 
    { _collection.Add(data); } 

    public IEnumerator GetEnumerator() 
    { 
     return _collection.GetEnumerator(); 
    } 
} 

public class Program 
{ 
    public void Example() 
    { 
     List<DataItem> repository = new List<DataItem>(){ 
      new DataItem() { 
       new Data("Name", "Mike"), 
       new Data("Age", 25), 
       new Data("BirthDate", new DateTime(1987, 1, 5)) 
      }, 
      new DataItem() { 
       new Data("Name", "Steve"), 
       new Data("Age", 30), 
       new Data("BirthDate", new DateTime(1982, 1, 10)) 
      } 
     }; 

     IEnumerable<DataItem> result = repository.AsQueryable<DataItem>().Where("it[\"Age\"] == 30"); 
     if (result.Count() == 1) 
      Console.WriteLine(result.Single()["Name"]); 
    } 

当我运行上面的例子,我得到:操作“==”与操作数类型的“对象”不兼容和“的Int32”

动态成员动态LINQ查询不兼容?或者是有处理动态

与会员处理正当评估时的另一种构建表达方式非常感谢您的帮助。

回答

2

动态成员动态LINQ查询不兼容?或者是有建设有型动态成员打交道时,将正确计算表达式的另一种方式?

两者都可以一起工作。只是做一个转换的Int32做像这样比较之前:

IEnumerable<DataItem> result = 
      repository.AsQueryable<DataItem>().Where("Int32(it[\"Age\"]) == 30"); 

编辑1:说了这么多,动态操作都没有表达允许使用动态的LINQ的连接结合在一般限制树木。考虑以下Linq-To-Objects查询:

IEnumerable<DataItem> result = repository.AsQueryable(). 
                Where(d => d["Age"] == 30); 

由于上述原因,此代码段不会编译。

编辑2:在你的情况下(和动态Linq一起),有一些方法可以解决编辑1和原始问题中提到的问题。例如:

// Variant 1: Using strings all the way 
public void DynamicQueryExample(string property, dynamic val) 
{ 
    List<DataItem> repository = new List<DataItem>(){ 
     new DataItem() { 
      new Data("Name", "Mike"), 
      new Data("Age", 25), 
      new Data("BirthDate", new DateTime(1987, 1, 5)) 
     }, 
     new DataItem() { 
      new Data("Name", "Steve"), 
      new Data("Age", 30), 
      new Data("BirthDate", new DateTime(1982, 1, 10)) 
     } 
    }; 

    // Use string comparison all the time   
    string predicate = "it[\"{0}\"].ToString() == \"{1}\""; 
    predicate = String.Format(whereClause , property, val.ToString()); 

    var result = repository.AsQueryable<DataItem>().Where(predicate); 
    if (result.Count() == 1) 
     Console.WriteLine(result.Single()["Name"]); 
} 

Program p = new Program(); 

p.DynamicQueryExample("Age", 30); // Prints "Steve" 
p.DynamicQueryExample("BirthDate", new DateTime(1982, 1, 10)); // Prints "Steve" 
p.DynamicQueryExample("Name", "Mike"); // Prints "Steve" (nah, just joking...) 

或:

// Variant 2: Detecting the type at runtime. 
public void DynamicQueryExample(string property, string val) 
{ 
    List<DataItem> repository = new List<DataItem>(){ 
     new DataItem() { 
      new Data("Name", "Mike"), 
      new Data("Age", 25), 
      new Data("BirthDate", new DateTime(1987, 1, 5)) 
     }, 
     new DataItem() { 
      new Data("Name", "Steve"), 
      new Data("Age", 30), 
      new Data("BirthDate", new DateTime(1982, 1, 10)) 
     } 
    }; 

    string whereClause = "{0}(it[\"{1}\"]) == {2}"; 


    // Discover the type at runtime (and convert accordingly) 
    Type type = repository.First()[property].GetType(); 
    string stype = type.ToString(); 
    stype = stype.Substring(stype.LastIndexOf('.') + 1); 

    if (type.Equals(typeof(string))) { 
     // Need to surround formatting directive with "" 
     whereClause = whereClause.Replace("{2}", "\"{2}\""); 
    } 
    string predicate = String.Format(whereClause, stype, property, val); 

    var result = repository.AsQueryable<DataItem>().Where(predicate); 
    if (result.Count() == 1) 
     Console.WriteLine(result.Single()["Name"]); 
} 

var p = new Program(); 
p.DynamicQueryExample("Age", "30"); 
p.DynamicQueryExample("BirthDate", "DateTime(1982, 1, 10)"); 
p.DynamicQueryExample("Name", "Mike"); 
+1

那他们不是真的'兼容'吗? – 2012-01-12 02:38:43

+0

谢谢,当我们事先知道该值的运行时类型时,该解决方案工作正常,但如何以编程方式构建查询时,我们不会真正知道如何在事先手动转换值。有没有其他方式可以建立一个表达式? – Darsegura 2012-01-12 03:33:45

+0

@ M.Babcock:取决于你对'兼容'的定义。 – afrischke 2012-01-12 10:38:34

1

你试过it[\"Age\"].Equals(object(30))

使得:

IEnumerable<DataItem> result = 
    repository.AsQueryable<DataItem>().Where("it[\"Age\"].Equals(object(30))"); 

编辑:更新正确投30到对象。

+0

引发异常”类型'System.Int32'的表达式不能用于'System'类型的参数。对象'方法'布尔等于(System.Object)'“。它它[\”Age \“]。Equals(object(30))''虽然。 – svick 2012-01-12 11:36:15

+0

答案更新相应。 – Seph 2012-01-12 12:01:14

+0

谢谢,此解决方案也运作良好。此解决方案与@afrischke提供的解决方案之间的任何性能考虑事项 – Darsegura 2012-01-12 19:13:34

1

下面的代码是否适合你?

IEnumerable<DataItem> result = repository.AsQueryable<DataItem>().Where("it[\"Age\"].ToString() == \"30\""); 

但对于这个工作,你的所有类型的可分配给您的Data类的Value成员需要有ToString方法的一个有用的执行。