EF4 Linq 返回类型通用列表

发布于 2024-10-03 09:02:18 字数 756 浏览 2 评论 0原文

我有一个使用 Entity Framework 4 的 C#-4 MVC3 RC 测试应用程序。

我有这个方法:

public static List<Content> FetchMenu(int websiteID) {
    return (from w in ContextHelper.Current.Websites
            where w.WebsiteID == websiteID
            select w.Contents).ToList();
}

此处涉及的对象(内容和网站)属于 EntityObject 类型。

上面的函数给出编译错误:

Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<Manager.Models.Content>>' to 'System.Collections.Generic.List<Manager.Models.Content>'. An explicit conversion exists (are you missing a cast?)

w.Contents是一个EntityCollection类型集合。

如何推迟 Linq.IQueryable 类型以返回内容类型的通用列表?

I have a C#-4 MVC3 RC test-application which is using Entity Framework 4.

I have this method:

public static List<Content> FetchMenu(int websiteID) {
    return (from w in ContextHelper.Current.Websites
            where w.WebsiteID == websiteID
            select w.Contents).ToList();
}

The objects involved here (Content and Website) are of type EntityObject.

The above function gives compilation error:

Cannot implicitly convert type 'System.Linq.IQueryable<System.Collections.Generic.List<Manager.Models.Content>>' to 'System.Collections.Generic.List<Manager.Models.Content>'. An explicit conversion exists (are you missing a cast?)

w.Contents is an EntityCollection<Content> type collection.

How do I defer the Linq.IQueryable type to return a generic List of type Content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白衬杉格子梦 2024-10-10 09:02:18

您需要使用括号,以便将 ToList() 应用于整个查询(IQueryable 类型的对象):

public static List<Content> FetchMenu(int websiteID) {
    return (from w in ContextHelper.Current.Websites
            where w.WebsiteID == websiteID
            select w.Contents).ToList();
}

否则您将调用 ToList()< /code> 仅适用于 w.Contents,然后应用 select。如果我显示方法链接语法可能会更清楚。

您的版本:

ContextHelper.
           Current.
           Websites.
           Where(w => w.WebsiteID == websiteID).
           Select(w => w.Contents.ToList());

正确版本:

ContextHelper.
           Current.
           Websites.
           Where(w => w.WebsiteID == websiteID).
           Select(w => w.Contents).
           ToList();

编辑

由于w.Contents是一个集合,因此您需要使用SelectMany将其展平:

public static List<Content> FetchMenu(int websiteID) {
    return ContextHelper.
           Current.
           Websites.
           Where(w => w.WebsiteID == websiteID).
           SelectMany(w => w.Contents).
           ToList();
}

You need to use parentheses, so that you apply ToList() to the whole query (an object of type IQueryable):

public static List<Content> FetchMenu(int websiteID) {
    return (from w in ContextHelper.Current.Websites
            where w.WebsiteID == websiteID
            select w.Contents).ToList();
}

Otherwise you are calling ToList() on w.Contents only and the select is applied afterwards. It might be clearer if I show the method chaining syntax.

Your version:

ContextHelper.
           Current.
           Websites.
           Where(w => w.WebsiteID == websiteID).
           Select(w => w.Contents.ToList());

Correct version:

ContextHelper.
           Current.
           Websites.
           Where(w => w.WebsiteID == websiteID).
           Select(w => w.Contents).
           ToList();

Edit:

Since w.Contents is a collection, you need to flatten it out by using SelectMany:

public static List<Content> FetchMenu(int websiteID) {
    return ContextHelper.
           Current.
           Websites.
           Where(w => w.WebsiteID == websiteID).
           SelectMany(w => w.Contents).
           ToList();
}
掩饰不了的爱 2024-10-10 09:02:18
    var query = (from w in ContextHelper.Current.Websites
                 where w.WebsiteID == websiteID
                 select w.Contents).First();

    return query.ToList();

.First() 似乎可以解决问题...谢谢。

    var query = (from w in ContextHelper.Current.Websites
                 where w.WebsiteID == websiteID
                 select w.Contents).First();

    return query.ToList();

The .First() seems to do the trick... thanks.

叫思念不要吵 2024-10-10 09:02:18

Yakimych 的答案使用 SelectMany() 是正确的。为了完整起见,这里使用“查询理解”语法:

public static List<Content> FetchMenu(int websiteID) {
    return (from w in ContextHelper.Current.Websites
            where w.WebsiteID == websiteID
            from c in w.Contents
            select c).ToList();
}

Yakimych's answer using SelectMany() is corret. For completeness, here it is using "query comprehension" syntax:

public static List<Content> FetchMenu(int websiteID) {
    return (from w in ContextHelper.Current.Websites
            where w.WebsiteID == websiteID
            from c in w.Contents
            select c).ToList();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文