Je dirai d'emblée que je fais des choses vraiment effrayantes avec linq sur les données dynamiques. Mais je ne peux pas comprendre pourquoi cette requête ne peut pas compiler:Erreur de compilation Dynamic + linq
Erreur 1 La propriété « <> h__TransparentIdentifier0 » ne peut pas être utilisé avec des arguments de type
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); } }
Pour ajouter l'insulte à l'injure, les travaux suivants :
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 };
Il est seulement quand j'ajoute:
où tag.ReferencedAggregate = null
que je reçois une erreur deux lignes avant:
où entity.Tags = erreur null // COMPILER ICI
Je ne sais pas ce qui se passe
Merci, c'était tout. J'ai fini de résoudre ceci en utilisant: de tag dans entity.Tags.Where ((Func t.ReferencedAggregate! = Null)) Ugly comme l'enfer. J'ai essayé d'écrire la méthode d'extension, mais je n'arrivais pas à comprendre comment la faire accepter par le CSC. –