首页 > 解决方案 > C# - LINQ - 在另一个 LINQ 查询的 WHERE 子句中使用列数据

问题描述

这是数据库图:数据库图

所以我执行这个查询:

            var query2 = from o in db.Orders
                     join od in db.Order_Details on o.OrderID equals od.OrderID
                     group new { o, od } by o.ShipCountry into oByCountry
                     orderby oByCountry.Sum(ood => ood.od.UnitPrice * ood.od.Quantity) descending
                     select new
                     {
                         Country = oByCountry.Key,
                         Total = oByCountry.Sum(ood => ood.od.UnitPrice * ood.od.Quantity)
                     };

我将结果放在 WPF 中的数据网格中。

GridOverview.ItemsSource = query2;

我想做的下一件事是,当我连续按下 GridOverview 时,我想显示居住在所选行所在国家/地区的所有客户以及每个客户的已售商品总价格。所以我在 var selectedRow 中定义了选定的项目。

var selectedRow = GridOverview.SelectedItem;

            var query = from c in db.Customers
                    join o in db.Orders on c.CustomerID equals o.CustomerID
                    join od in db.Order_Details on o.OrderID equals od.OrderID
                    **where c.Country == selectedRow.**
                    group new {c, o, od} by c.CompanyName into cByName
                    orderby cByName.Sum(ood => ood.od.UnitPrice * ood.od.Quantity) descending
                    select new
                    {
                        cByName.Key,
                        Total = cByName.Sum(ood => ood.od.UnitPrice * ood.od.Quantity)
                    };

现在我的问题是:如何在 where 子句中将 selectedRow 的 Country 与 C.Country 进行比较?selectedRow.Country 没有成功,但这可能不是它的工作方式。

提前致谢!

标签: c#linq

解决方案


通常,这是您将在 LINQ 中进行比较的方式。有2种可能的方式。只需使用“==”或“.Equals”即可。

customers.Where(c => c.country == selectedRow.Country);

或者

customers.Where(c => c.country.Equals(selectedRow.Country));

您可能必须使用“ToLower()”函数或“ToLowerInvariant()”函数。我不知道你的项目的背景,所以你会看看它是否对你有好处。


推荐阅读