如何使用Linq和ASP.Net Forms在多个表之间编写连接(How do I write Joins between multiple tables with Linq and ASP.Net

编程入门 行业动态 更新时间:2024-10-22 23:05:56
如何使用Linq和ASP.Net Forms在多个表之间编写连接(How do I write Joins between multiple tables with Linq and ASP.Net Forms)

我已经实现了一个有效的解决方案,但LINQ对我来说是一件好事,我很难找到所需的信息。

这是我有的:

var prodCat = from pc in db.ProductCategories where pc.Category == Int32.Parse(CategoriesDropper.SelectedValue) select pc; List<int> productIds = new List<int>(); foreach (var pc in prodCat) { productIds.Add(pc.Product); } var products = from p in db.Products where productIds.Contains(p.Id) select p; ProductsGridView.DataSource = products; ProductsGridView.DataBind();

理想情况下,我想用一个选择而不是两个选择来获得相同的结果。

I've implemented a solution that works, but LINQ is a something nre for me and I am struggling to find the required information.

Here is what I have:

var prodCat = from pc in db.ProductCategories where pc.Category == Int32.Parse(CategoriesDropper.SelectedValue) select pc; List<int> productIds = new List<int>(); foreach (var pc in prodCat) { productIds.Add(pc.Product); } var products = from p in db.Products where productIds.Contains(p.Id) select p; ProductsGridView.DataSource = products; ProductsGridView.DataBind();

Ideally I would like to achieve the same result with a single select rather than two.

最满意答案

var products = from pc in db.ProductCategories join p in db.Products on pc.Product equals p.Id where pc.Category == Int32.Parse(CategoriesDropper.SelectedValue) select p; ProductsGridView.DataSource = products; ProductsGridView.DataBind();

有关Visual Studio 2010的信息 ,请参阅CQ中的LINQ - Join Operators以供参考。

希望这可以帮助!

var products = from pc in db.ProductCategories join p in db.Products on pc.Product equals p.Id where pc.Category == Int32.Parse(CategoriesDropper.SelectedValue) select p; ProductsGridView.DataSource = products; ProductsGridView.DataBind();

See LINQ - Join Operators in C# for Visual Studio 2010 for reference.

Hope this helps!

更多推荐

本文发布于:2023-07-14 20:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何使用   Net   ASP   Forms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!