首页 文章

动态linq编译错误

提问于
浏览
19

我会在前面说我在动态数据上使用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 //编译器错误在这里

不知道发生了什么

3 回答

  • 1

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

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

  • 13
    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

    匿名类型返回是<> h__TransparentIdentifier0并且在编译时由编译器处理 - 问题显示为"dynamic order of precedence" - 在此处读取:Method-missing difficulties in C# 4.0: dynamic vs RealProxy

    我今天刚刚完成了这篇文章 . 我将有一个小猜测,并说动态分配后准备匿名类型:) - 编译器知道这一点,并挫败你 .

    如果使用常规类型返回,问题是否会消失?我猜它必须 .

相关问题