我會先說我正在做一些關於linq動態數據的非常可怕的事情。 但我不明白,爲什麼這個查詢失敗編譯:Dynamic + linq編譯錯誤
錯誤1酒店「<> h__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 = NULL
我得到一個錯誤的兩行之前:
其中entity.Tags = NULL //編譯器錯誤HERE
不知道什麼是對
謝謝,就是這樣。 ((Func)(t => t.ReferencedAggregate!= null)) 醜陋如地獄。 我試圖在那裏寫擴展方法,但我無法弄清楚如何讓CSC接受它。 –
2010-08-10 01:09:00