使用linq2SQL返回一对多关系?
有没有一种从 linq2sql 返回一对多关系的好方法,可能需要一些解释! :-)
基本上我有一个包含发票的表和另一个包含发票详细信息的表。
我有使用 linq2sql 设计器自动创建的 linq2sql 类...
因此要返回发票 1232 有 15 个不同项目/详细信息的查询。 ..我现在看到的方式是我必须对发票进行查询,然后对发票查询详细信息进行查询...
我确信一定有更简单的方法? 而不是进行 2 个查询……即 JOIN?
我还想看看是否可以返回 Iqueryable 中的值,但带有链接表?
基本上我有我的方法,即 Iqueryable<> 获取发票();
当然, Iqueryable 是通用的,可以包含发票或发票详细信息 - 不能同时包含两者,或者也许我遗漏了一些东西?
is there a good way of of returning a 1 to many relationship from linq2sql, probably needs some explanation ! :-)
Basically i have a table that has invoices and another table that is invoice details..
I have my linq2sql classes that were automatically created using linq2sql designer ...
So to return the query where invoice 1232 has 15 different items/details... The way i see it at the moment is that i have to Do a query on the invoice and then a query on the invoice query detail...
I am sure there must be an easier way ? rather than doing 2 queries ... i.e. a JOIN?
Also i wanted to see if i could return the values in a Iqueryable but with the linked table?
Basicall i have my method that is Iqueryable<> GetInvoice();
of course Iqueryable is a generic and can contain Invoice or Invoice Details - NOT both, or maybe i am missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的数据库设计已包含发票表中的主键与发票详细信息表中的外键(发票 ID)之间的连接(“约束”),那么您应该能够从发票对象访问发票详细信息像这样:
当您将表拖到设计器中时,Linq to SQL 应该已经为您创建了这种关系。 换句话说,您可以从发票对象中直接访问详细信息。
如果此关系确实存在,您应该在 Linq to SQL 设计器中将其视为发票表和发票详细信息表之间绘制的一条线。
If your database design already contains a connection ('constraint') between the primary key in your invoices table, and the foreign key (invoice id) in your invoice details table, then you should be able to access your invoice details from your invoice object like this:
Linq to SQL should have created this relationship for you when you dragged your tables into the designer. In other words, you can reach out from your Invoice object, and access the details directly.
If this relationship does exist, you should see it in the Linq to SQL designer as a line drawn between the invoices table and the invoice details table.
如果您在数据库中设置了关系,然后使用设计器创建了对象,那么您应该能够自动从发票中获取发票详细信息。
像这样的东西......
If you have your relationships set up in your db, then you created you objects using the designer, you should automatically be able to get invoice details from an invoice.
something like this....