Dirò in anticipo che sto facendo cose davvero spaventose con linq su dati dinamici. Ma io non riesco a capire perché questa domanda non riesce a compilare:Errore di compilazione Dynamic + linq
di errore 1 La proprietà '<> h__TransparentIdentifier0' non può essere utilizzato con argomenti di tipo
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); } }
Per aggiungere la beffa al danno, le seguenti opere :
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 };
E 'solo quando aggiungo:
dove tag.ReferencedAggregate = null
che ottengo un errore due righe prima:
dove entity.Tags = null // Errore del compilatore QUI
Non so cosa sta succedendo
Grazie, era così. Ho risolto il problema utilizzando: dal tag in entity.Tags.Where ((Func) (t => t.ReferencedAggregate! = Null)) Brutto da morire. Ho provato a scrivere il metodo di estensione lì, ma non sono riuscito a capire come rendere CSC accettarlo. –