首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Dynamic+Linq编译错误?

Dynamic+Linq编译错误?
EN

Stack Overflow用户
提问于 2018-02-05 00:09:48
回答 2查看 0关注 0票数 0

我在Dynamic上使用Linq。但是我不明白为什么这个查询不能编译:

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

代码语言:txt
复制
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);

    }
}

更糟的是:

代码语言:txt
复制
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 };

当我加上:

where tag.ReferencedAggregate != null

我又得到了一个错误:

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

EN

回答 2

Stack Overflow用户

发布于 2018-02-05 08:21:25

如果我试着:

代码语言:txt
复制
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)

我得到了一个不同的编译器错误:

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

票数 0
EN

Stack Overflow用户

发布于 2018-02-05 09:16:43

代码语言:txt
复制
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 };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档