如何在 .NET Ria 服务中查询 Linq to Entity 框架中的关联
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 GetItems 对我来说看起来不错。你也可以这样做:
两者都应该返回相同的东西。
Your GetItems looks fine to me. You could also do:
Both should return the same thing.
您可以让父实体包含子实体。为此,您必须执行 2 件事:
1) 更新您的域查询以包含文件夹项目和项目:
2) 更新元数据文件,以便 RIA 服务知道将关联返回给客户端:
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:
2) Update the metadata file so that the RIA service knows to return the associations to the client: