如何使用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!
更多推荐
发布评论