动态+ linq编译错误

时间:2010-08-09 22:51:57

标签: c# 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 // COMPILER ERROR HERE

不确定发生了什么

3 个答案:

答案 0 :(得分:13)

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

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运算符。

答案 1 :(得分:2)

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

那好一点。不完美,但它就像开始一样 - 你只能在陷入困境之前走得那么深。

答案 2 :(得分:1)

匿名类型返回是&lt;&gt; h__TransparentIdentifier0并且在编译时由编译器处理 - 问题显示为“动态优先顺序” - 请在此处阅读: Method-missing difficulties in C# 4.0: dynamic vs RealProxy

我今天刚刚完成了这篇文章。我将有一个小小的猜测,并说在动态分配后,准备了 - 编译器知道这一点并且正在挫败你。

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