C# 动态+linq编译错误

C# 动态+linq编译错误,c#,linq,dynamic,C#,Linq,Dynamic,我要说的是,我在动态数据上使用linq做了一些非常可怕的事情。 但我不明白为什么这个查询无法编译: 错误1属性“h\uu TransparentIdentifier0”不能与类型参数一起使用 public class Program { public static void Main(string[] args) { var docs = new dynamic[0]; var q = from doc in docs

我要说的是,我在动态数据上使用linq做了一些非常可怕的事情。 但我不明白为什么这个查询无法编译:

错误1属性“h\uu TransparentIdentifier0”不能与类型参数一起使用

public class Program { public static void Main(string[] args) { var docs = new dynamic[0]; var q = from doc in docs where doc["@metadata"]["Raven-Entity-Name"] == "Cases" where doc.AssociatedEntities != null from entity in doc.AssociatedEntities where entity.Tags != null // COMPILER ERROR HERE from tag in entity.Tags where tag.ReferencedAggregate != null select new {tag.ReferencedAggregate.Id, doc.__document_id}; } } public static class LinqOnDynamic { private static IEnumerable<dynamic> Select(this object self) { if (self == null) yield break; if (self is IEnumerable == false || self is string) throw new InvalidOperationException("Attempted to enumerate over " + self.GetType().Name); foreach (var item in ((IEnumerable) self)) { yield return item; } } public static IEnumerable<dynamic> SelectMany(this object source, Func<dynamic, int, IEnumerable<dynamic>> collectionSelector, Func<dynamic, dynamic, dynamic> resultSelector) { return Enumerable.SelectMany(Select(source), collectionSelector, resultSelector); } public static IEnumerable<dynamic> SelectMany(this object source, Func<dynamic, IEnumerable<dynamic>> collectionSelector, Func<dynamic, dynamic, dynamic> resultSelector) { return Enumerable.SelectMany(Select(source), collectionSelector, resultSelector); } public static IEnumerable<dynamic> SelectMany(this object source, Func<object, IEnumerable<dynamic>> selector) { return Select(source).SelectMany<object, object>(selector); } public static IEnumerable<dynamic> SelectMany(this object source, Func<object, int, IEnumerable<dynamic>> selector) { return Select(source).SelectMany<object, object>(selector); } } 雪上加霜的是,以下工作:

var docs = new dynamic[0]; var q = from doc in docs where doc["@metadata"]["Raven-Entity-Name"] == "Cases" where doc.AssociatedEntities != null from entity in doc.AssociatedEntities where entity.Tags != null from tag in entity.Tags select new { tag.ReferencedAggregate.Id, doc.__document_id }; 只有当我添加以下内容时:

其中tag.ReferencedAggregate!=空的

我在两行之前得到一个错误:

where entity.Tags!=此处为null//编译器错误


不确定发生了什么

匿名类型返回是h_uTransparentIdentifier 0,由编译器在编译时处理-问题似乎是动态优先顺序-请阅读此处:

我今天刚刚在最近的一篇文章中谈到了这一点。我会有一个小小的猜测,并说匿名类型是在动态赋值之后准备的:-编译器知道这一点,并且正在阻挠您


如果使用常规类型返回,问题是否会消失?我想一定是这样。

如果我尝试将您的电话转换为:

var q = from doc in docs.Where(doc => doc["@metadata"]["Raven-Entity-Name"] == "Cases" || doc.AssociatedEntities != null)
        from entity in doc.AssociatedEntities.Where(entity => entity.Tags != null)
我得到一个不同的编译器错误,它可能揭示了发生的情况:

'如果不首先将lambda表达式强制转换为委托或表达式树类型,则无法将其用作动态调度操作的参数'

所以我想你必须让Where操作符超负荷工作

var q = from doc in docs where doc["@metadata"]["Raven-Entity-Name"] == "Cases" where doc.AssociatedEntities != null from entity in ((IEnumerable<dynamic>)doc.AssociatedEntities) .Where(entity => entity.Tags != null) from tag in ((IEnumerable<dynamic>)entity.Tags) .Where(tag => tag.ReferencedAggregate != null) select new { tag.ReferencedAggregate.Id, doc.__document_id };
这样好一点。不完美,但它就像《盗梦空间》——在你迷失在边缘之前,你只能深入这么多层次。

Rob,我不这么认为。主要问题是它适用于许多其他场景。我不明白为什么它对这个不起作用。所有LinqOnDynamic方法都返回动态的IEnumerable,因此这应该不是问题。看起来它试图对扩展方法进行强绑定,但我不知道如何。啊-dynamic不支持ExtensionMethods:我不能确切地告诉您在哪里使用它-但是yah扩展方法可能是个问题。这实际上不是在dynamic上运行的,而是在静态类型的dynamic的IEnumerable上运行的,所以它可以有扩展方法。谢谢,就是这样。我在entity.Tags.WhereFunct=>t.ReferencedAggregate!=丑得要命。我尝试在那个里编写扩展方法,但我不知道如何让CSC接受它。