这是我的表结构 t_physical_inventory
dte_cycle_count | qty | location
我正在使用以下Linq查询来获得结果
int number = 3;
List<t_physical_inventory> Location = (from c in dc.t_physical_inventories
where (c.dte_cycle_count == null ||
((TimeSpan)(Convert.ToDateTime(c.dte_cycle_count) -
(DateTime.Now.Date))).Days > 365)
&& (c.qty > 0)
orderby (c.location)
select c).Take(number).ToList();
问题: 我无法获得dte_cycle_count大于365的记录。
答案 0 :(得分:2)
尝试:
int number = 3;
List<t_physical_inventory> Location = null;
Location = (from c in dc.t_physical_inventories
where (c.dte_cycle_count == null
|| (DateTime.Now.Date - Convert.ToDateTime(c.dte_cycle_count))
).Days > 365)
&& (c.qty > 0)
orderby (c.location)
select c).Take(number).ToList();
答案 1 :(得分:1)
使用TotalDays
作为Timespan,1年12天从Days
返回12,但TotalDays
返回Years * 365 + 12
如果Yahya建议,你应该从较晚的日期减去较早的日期。
答案 2 :(得分:0)
时间跨度是否正面?您可以使用Duration
-method获取绝对值:
List<t_physical_inventory> Location = null;
Location = (from c in dc.t_physical_inventories
where (c.dte_cycle_count == null
|| (c.dte_cycle_count.Value - DateTime.Now.Date).Duration.Days > 365)
&& (c.qty > 0)
orderby (c.location)
select c).Take(number).ToList();
然而,这对于积极的时间跨度与消极时间相同。最好先从后面的DateTime
中减去先前的DateTime
。
答案 3 :(得分:0)
请注意,如果您希望数据一年,则需要采用不同的方式:
List<t_physical_inventory> Location = null;
Location = (from c in dc.t_physical_inventories
where (c.dte_cycle_count == null || c.dte_cycle_count.Value < DateTime.Now.AddYears(-1)) && (c.qty > 0)
orderby (c.location)
select c).Take(number).ToList();