动态+ 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 //编译器错误在这里

不知道发生了什么事


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

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 };

这会好一点。 不完美,但它就像开始 - 在你陷入困境之前,你只能深入到很多层次。


匿名类型的返回值是<> h__TransparentIdentifier0,并在编译时由编译器进行处理 - 问题显示为“动态优先顺序” - 请在此处阅读:在C#4.0中缺少方法 - 困难:dynamic vs RealProxy

我刚刚在最近的一篇文章中讨论过这个问题。 我会有一个小猜测,并说Anonymous类型是在动态赋值之后准备的:) - 编译器知道这一点并阻止了你。

如果您使用常规类型的退货,问题是否会消失? 我猜是必须的。

链接地址: http://www.djcxy.com/p/53759.html

上一篇: Dynamic + linq compilation error

下一篇: Is it better to yield return lookup lists or preload a static list in c#?