如何在 .NET Ria 服务中查询 Linq to Entity 框架中的关联

发布于 2024-08-04 03:38:00 字数 1322 浏览 0 评论 0原文

我刚刚开始使用 Linq 和 Linq to Entity Framewok。除此之外还有 .NET Ria 服务。 我的问题是,我有两个表Folder和Item,它们使用第三个“连接”表FolderItem,具有多对多关系,如下所示:
(来源:InsomniacGeek.com

在.NET RIA Service 域服务中,我想创建一个返回给定FolderID 的所有项目的方法。

在 T-SQL 中,会是这样的:

SELECT * FROM Item i
INNER JOIN FolderItem fi ON fi.ItemID = i.ID
WHERE fi.FolderID = 123

我的 Linq 知识有限,但我想做这样的事情:

public IQueryable<Item> GetItems(int folderID)
{
  return this.Context.Items.Where(it => it.FolderItem.ID == folderID);
}

这不是正确的语法,它给出了这个错误:

无法将 lambda 表达式转换为 输入“字符串”,因为它不是 委托类型

执行此操作的正确方法是什么(使用关联)?

我可以以某种方式使用 .Include("FolderItem") 吗?

请仅使用方法语法。

附言。 使用查询表达式的情况如下:

  public IQueryable<Item> GetItemsByFolderID(int folderID)
  {
    return from it in this.Context.Items
           from fi in it.FolderItem
           where fi.Folder.ID == folderID
           select it;
  }

问题是,使用基于方法的查询语法会是什么样子?

I have just started with Linq and Linq to Entity Framewok. On top of that with the .NET Ria services.
My problem is that I have 2 tables Folder and Item with a many to many relationsship using a third "connection" table FolderItem like this:

(source: InsomniacGeek.com)

In the .NET RIA Service domain service, I want to create a method that returns all Items for a given FolderID.

In T-SQL , that would something like this:

SELECT * FROM Item i
INNER JOIN FolderItem fi ON fi.ItemID = i.ID
WHERE fi.FolderID = 123

My Linq knowledge is limited, but I want to do something like this:

public IQueryable<Item> GetItems(int folderID)
{
  return this.Context.Items.Where(it => it.FolderItem.ID == folderID);
}

This is not the correct syntax, it gives this error:

Cannot convert lambda expression to
type 'string' because it is not a
delegate type

What is the correct way of doing this (with associations) ?

Can I user the .Include("FolderItem") somehow?

Please, method syntax only.

PS.
Here's how it would look like using a Query Expression:

  public IQueryable<Item> GetItemsByFolderID(int folderID)
  {
    return from it in this.Context.Items
           from fi in it.FolderItem
           where fi.Folder.ID == folderID
           select it;
  }

The qeustion is, how would it look like using the Method Based Query Syntax?

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

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

发布评论

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

评论(2

紫罗兰の梦幻 2024-08-11 03:38:00

你的 GetItems 对我来说看起来不错。你也可以这样做:

public IQueryable<Item> GetItems(int folderID)
{
    return this.Context.FolderItems
                       .Where(fi => fi.ID == folderID)
                       .Select(fi => fi.Items);
}

两者都应该返回相同的东西。

Your GetItems looks fine to me. You could also do:

public IQueryable<Item> GetItems(int folderID)
{
    return this.Context.FolderItems
                       .Where(fi => fi.ID == folderID)
                       .Select(fi => fi.Items);
}

Both should return the same thing.

兮子 2024-08-11 03:38:00

您可以让父实体包含子实体。为此,您必须执行 2 件事:

1) 更新您的域查询以包含文件夹项目和项目:

return from x in Context.FolderItems
                        .Include("FolderItem")
                        .Include("FolderItem.Item")
       where x.ID == folderID
       select x

2) 更新元数据文件,以便 RIA 服务知道将关联返回给客户端:

[MetadataTypeAttribute(typeof(FolderMetadata))]
public partial class Folder
{
     internal sealed class FolderMetadata
     {
           ...
           [Include]
           public FolderItem FolderItem; 
     }
}

You can have the parent entity contain the child entities. There are 2 things you have to do to do this:

1) Update your domain query to include the folder items and items:

return from x in Context.FolderItems
                        .Include("FolderItem")
                        .Include("FolderItem.Item")
       where x.ID == folderID
       select x

2) Update the metadata file so that the RIA service knows to return the associations to the client:

[MetadataTypeAttribute(typeof(FolderMetadata))]
public partial class Folder
{
     internal sealed class FolderMetadata
     {
           ...
           [Include]
           public FolderItem FolderItem; 
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文