c# - 使用 Lambda 或 Linq 根据子表获取数据
问题描述
我有两张表,它们的产品和价格之间存在一对多的关系。只有当价格表没有超过一个月的相关记录或价格更新日期时,我才想从产品表中获取前 5 个项目。
我试过这个;
DateTime checkdate = DateTime.Today.AddMonths(-1);
var test = (from p in main.product
from r in main.price
where !p.price.Any() && r.UpdateTime >= checkdate
select p.main.productid).Take(5);
//webservice will check new prices here
所以,每次都返回相同的记录......
解决方案
由于您的条件包含或“仅当价格表没有相关记录或价格更新日期超过一个月时。 ”您需要在 where 条件下将运算符 AND ( &&
) 更改为 OR ( )。||
var test = (from p in main.product
from r in main.price
where !p.price.Any() || r.UpdateTime >= checkdate
select p.main.productid).Take(5);
推荐阅读
- python - 如何覆盖在其父类中使用的子类中的属性?
- oembed - 如何将 oembed API 与 soundcloud 一起使用?
- php - 当请求来自移动设备时 session_start() 无法访问 /tmp 目录
- python - 有没有办法为python的cmd模块中的函数添加值(使用程序时)
- python - 从 Lambda 函数 (AWS) 的用户列表中获取 UserId
- git - 使用两个不同的 Github 帐户
- python - 将列表值合并到 1 个字符串中
- regex - Python:删除所有以大写字母开头且标点符号后不出现的单词
- html - 如何在angular6中将记录从一个网格推送到另一个网格
- typescript - Mongoose 的 loadClass() 和 TypeScript