如何使用linq找到最大值 [英] How to find max using linq
问题描述
如何使用linq查找max。我需要按条件date.DateTypeID == 93 ||过滤日期表中的数据。 date.DateTypeID == 320 || date.DateTypeID == 332.但我需要从这里拿出最大日期。我为此写了一个linq。
How to find max using linq.I need to filter the data from Dates table by conditions date.DateTypeID == 93 || date.DateTypeID == 320 || date.DateTypeID == 332.But i need to take the maximum date from this.I hve written linq for this.
var statusDates = from date in userEnt.Dates
group date by date.DateTypeID into g
select new
{
DateID = g.Key,
sDate = g.Max(k=>Convert.ToDateTime(k.Date1))
};
但是我得到了如下错误。
LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.Object)'方法,且此方法无法转换为int oa store expression.Help meee
But i got errror as following.
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.Object)' method, and this method cannot be translated into a store expression.Help meee
推荐答案
没有通用的Object to DateTime转换 - 所以你得到了错误。
我不知道知道你的Date1属性是什么类型,但如果你先显式地转换它(实际上是它的类型)它应该工作:
There is no generic Object to DateTime conversion - so you get the error.
I don't know what type your Date1 property is, but if you explicitly cast it first (to the type it actually is) it should work:
g.Max(k=>Convert.ToDateTime((string) k.Date1))
例如。
如果已经是DateTime ,然后只使用显式演员,它应该没问题。
For example.
If it's already a DateTime, then just use the explicit cast and it should be fine.
g.Max(k=>(DateTime) k.Date1)
参考
how-to-find-item-with-max-value-using-linq [ ^ ]
这篇关于如何使用linq找到最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!