c# - 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 没有成功,但这可能不是它的工作方式。
提前致谢!
解决方案
通常,这是您将在 LINQ 中进行比较的方式。有2种可能的方式。只需使用“==”或“.Equals”即可。
customers.Where(c => c.country == selectedRow.Country);
或者
customers.Where(c => c.country.Equals(selectedRow.Country));
您可能必须使用“ToLower()”函数或“ToLowerInvariant()”函数。我不知道你的项目的背景,所以你会看看它是否对你有好处。
推荐阅读
- node.js - crypto.getCiphers() - 为什么通过 NodeJS (v12.18.3) 与 ElectronJS 通过 (NodeJS 12.14.1) 提供一组不同的算法?
- jenkins - Jenkinsfile 并行阶段 - 一个说在运行时暂停
- vue.js - 如何在 nuxt axios instanse 中添加令牌
- python - 如何使用日期作为过滤器将列添加到数据框中
- docker - 如何通过“docker logs”获取嵌入在 docker 容器中的 VM 的控制台日志
- scala - 如何解决编译时的类型错误:Any to T?
- mysql - MySQL 根据存储过程中传递的 id 添加新的 LIST 分区?
- c++ - 我的应用程序名称在运行后更改,但它没有文件扩展名,我希望它在 .exe 中
- c++ - 数独求解器生成值为 50+ 的单元格
- c# - 我是否正确理解如何通过 RSA + AES 加密 TCP 流量?