[英]how to use if condtion in linq c#?
I have a table of data having startdate
, enddate
and time for example例如,我有一个包含startdate
、 enddate
和 time 的数据表
startdate enddate time
03/19/2020 03/19/2020 15:23
03/19/2020 03/20/2020 12:26
03/19/2020 03/19/2020 05:23
03/19/2020 03/19/2020 09:23
03/19/2020 03/21/2020 04:23
I want to use LINQ query to get a minimum of time when startdate = enddate我想使用 LINQ 查询来获取 startdate = enddate 的最短时间
I use this query我使用这个查询
time = table.Where(x => x.StartDate == x.EndDate).Min(x=>x.time)
but when I run this code it gives me exception InValidOperationException was unhandled by user code但是当我运行此代码时,它给了我异常 InValidOperationException 未被用户代码处理
another query I have to use is我必须使用的另一个查询是
time = table.OrderBy(x=>x.StartDate == x.EndDate).First().Time
but it is giving me an incorrect output但它给了我一个不正确的 output
Any suggestion how can I fix this任何建议我该如何解决这个问题
You can try this way你可以试试这个方法
time = table.OrderBy(x => x.time).FirstOrDefault(x => x.StartDate == x.EndDate)?.time;
Please check if your data is really equal, they may contain invisible blank characters.请检查您的数据是否真的相等,它们可能包含不可见的空白字符。 If that were the case, it would lead to the problem.如果是这样的话,就会导致问题。 you can use table.ToList()
and set a breakpoint to debug.您可以使用table.ToList()
并设置断点进行调试。
Maybe:也许:
time = table.Where(e => e.StartDate == e.EndDate).OrderBy(e => e.Time).FirstOrDefault()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.