动态+ LINQ编译错误 [英] Dynamic + linq compilation error

查看:103
本文介绍了动态+ LINQ编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会说起来,我正在使用linq对动态数据做一些非常可怕的事情。
但是我不知道为什么这个查询无法编译:



错误1属性'<> h__TransparentIdentifier0'不能与类型参数

 
public class Program
{
public static void Main(string [] args)
{
var docs = new dynamic [0];
var q = from doc in docs
其中doc [@ metadata] [Raven-Entity-Name] ==Cases
其中doc.AssociatedEntities!= null
从实体在doc.AssociatedEntities
其中entity.Tags!= null // COMPILER ERROR HERE
从标签在entity.Tags
其中tag.ReferencedAggregate!= null
选择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(尝试枚举+ self.GetType()。Name);

foreach(var item in((IEnumerable)self))
{
yield return item;
}
}

public static IEnumerable< dynamic> SelectMany(这个对象源,
Func< dynamic,int,IEnumerable< dynamic>> collectionSelector,
Func< dynamic,dynamic,dynamic> resultSelector)
{
return枚举。 SelectMany(Select(source),collectionSelector,resultSelector);
}

public static IEnumerable< dynamic> SelectMany(此对象源,
Func< dynamic,IEnumerable< dynamic>> collectionSelector,
Func<动态,动态> resultSelector)
{
return Enumerable.SelectMany选择(来源),collectionSelector,resultSelector);
}

public static IEnumerable< dynamic> selectMany(此对象源,
Func< object,IEnumerable< dynamic>>选择器)
{
return Select(source).SelectMany< object,object>(selector);
}

public static IEnumerable< dynamic> selectMany(此对象源,
Func< object,int,IEnumerable< dynamic>>选择器)
{
return Select(source).SelectMany< object,object>(selector);

}
}

为了增加侮辱伤害,以下作品:

 
var docs = new dynamic [0];
var q = from doc in docs
其中doc [@ metadata] [Raven-Entity-Name] ==Cases
其中doc.AssociatedEntities!= null
from entity in doc.AssociatedEntities
其中entity.Tags!= null
来自entity.Tags中的标签
select new {tag.ReferencedAggregate.Id,doc .__ document_id};

只有当我添加:



其中tag.ReferencedAggregate!= null



我之前收到两行错误:



其中entity.Tags!= null // COMPILER



不知道发生了什么

解决方案

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

  var q = from doc in docs.Where(doc => doc [@ metadata ] [Raven-Entity-Name] ==Cases|| doc.AssociatedEntities!= null)
来自doc.AssociatedEntities.Where中的实体(entity => entity.Tags!= null)

我收到一个不同的编译器错误,这可能揭示了发生了什么:



'不能使用lambda表达式作为动态调度操作的参数,而无需首先将其转换为委托或表达式树类型'



所以我猜测你必须重载Where运算符。


I'll say up front that I am doing some really scary things with linq on dynamic data. But I can't figure out why this query fails to compile:

Error 1 The property '<>h__TransparentIdentifier0' cannot be used with type arguments

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

    }
}

To add insult to injury, the following works:

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

It is only when I add:

where tag.ReferencedAggregate != null

That I get an error two lines before:

where entity.Tags != null // COMPILER ERROR HERE

Not sure what is going on

解决方案

If I try just converting your calls to:

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)

I get a different compiler error which perhaps reveals what is going on:

'Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type'

So I guess you have to overload the Where operator.

这篇关于动态+ LINQ编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆