开发者

Selecting DateTime from a query

I have the following linq to sql query:

 DateTime linqdate = (from de in dvlist
                                  where de.DataValue == value
                                  select de.DateTime);

I w开发者_Go百科ant to get the date value form database in a datetime variable but I got the following error:

cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.DateTime'

any ideas where the problem is? thanks in advance


A Linq query returns an IEnumerable<T> that you can iterate or you can convert it to another type of object (using some extension methods)

In order to get what you want you should do something like this :

var dateTime=(from de in dvlist 
              where de.DataValue == value 
              select de.DateTime).FirstOrDefault();

this way you are returning the first element of your enumerable object, or the default value for that type (T, in this case DateTime) if there is no match in the query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜