score:9
That's because Time2 is a nullable type. Nullable types always have a value property. If you revise your line to:
Time2 = (a.Time2).Value
and then press the period symbol after Value, you'll see the Intellisense pop up.
As a safety check you may want to also check that the value of a.Time2 is not null, before using .Value. Otherwise you'll get a null reference exception
score:1
Why do you think that You should have a method that exists for items of a collection on something that is a System.Nullable<DateTime>
.
You won't have SingleOrDefault()
on this.
Time2 = (a.Time2.HasValue ? a.Time2.Value : null)
That's how you should set the value or have it be null.
score:3
Time2 = a.Time2.HasValue ? a.Time.Value : null;
score:4
SingleOrDefault is a extension method for the generic IEnumerable as you see below.
public static TSource SingleOrDefault(
this IEnumerable source)
If you had some list of Nullable you could use that method and it would return the first value or throws if the list had none or more than one item.
//a list of Nullable<DateTime> with exactly 1 item
var listOfDates = new DateTime?[] { null };
var myDate = listOfDates.SingleOrDefault(); // myDate => ((DateTime?)null)
That is probably not what you want, you want some value when it is null, then you should use either the coalesce operator of C# 3.0 or one of the overloads of the GetValueOrDefault method from Nullable<> type.
DateTime? myNullDate = null;
DateTime myDate;
myDate = myNullDate ?? DateTime.Now; // myDate => DateTime.Now
myDate = myNullDate ?? default(DateTime); // myDate => DateTime.MinValue
myDate = myNullDate.GetValueOrDefault(DateTime.Now); // myDate => DateTime.Now
myDate = myNullDate.GetValueOrDefault(); // myDate => DateTime.MinValue
Source: stackoverflow.com
Related Articles
- Why can't I cast nullable DateTime as string in a LinQ query?
- how does one compare the date part of a nullable datetime in a linq / lambda query?
- LINQ Source Code Available
- Dynamic LINQ (to entities) Where with nullable DateTime column
- Linq DateTime expressions wont compare if only one is nullable
- Filter by nullable datetime field using Linq
- linq on nullable datetime
- linq to get max of nullable datetime from a list
- linq with nullable datetime
- creating Linq to sqlite dbml from DbLinq source code
- Case statement in Linq for a nullable datetime field
- LINQ to Entities does not recognize the method 'System.String ToString()' method when converting Nullable DateTime
- Possible InvalidOperationException when working with Nullable DateTime in LINQ
- Linq insert with nullable datetime column
- How can I remove the time part from a nullable datetime in where condition of Linq
- source code for LINQ 101 samples
- Error while converting nullable datetime to string in LINQ asp.net MVC
- Nullable DateTime from SQL database into Datatable using generics and Linq
- Linq Expression For DateTime Equality Nullable Issue
- Nullable DateTime in Where Clause in EF Linq Query
- How to Parse a nullable DateTime during a LINQ group operation?
- How to return nullable datetime in ef linq query
- LINQ filter nullable datetime with matching params?
- Check Nullable for object insde LinQ to Sql code
- c# Linq or code to extract groups from a single list of source data
- Ternary operator in LINQ queries used for handle nullable DateTime throws the exception
- Comparing Nullable Datetime got error Only parameterless constructors and initializers are supported in LINQ to Entities
- Linq to EF: find the nearest Nullable Datetime from now
- Error convert Datetime to nullable Datetime in Linq
- Linq grouping by nullable datetime and using this as criteria
- Creating a plan repository, but using keyword in constructor body
- Linq Into Clause Missing Field
- Inner Joining Tables with 2 different variables
- How do you set a type to an IEnumerable<> if the results will have many types?
- Comparing two lists to get objects that appear in both
- Roll up totals of an ObservableCollection<MyType> with a Linq expression
- Binding Combobox
- Detect where applied to IQueryable<T>
- How can I code numerous MIN functions into one LINQ to DataSet query
- Linq/Enumerable Any Vs Contains
- Iterator bug of (more likely) a very subtle behavior?
- How to express LEFT JOIN when 2 entities are linked by a navigation properties
- Best way of checking Null in C# 4.0
- testing for no return from LINQ
- Linq get element from string list and a position of a char in this list
- LINQ2SQL doesn't return row if checking with null
- Create anonymous object via LINQ lambda from one result
- cannot convert to system.guid
- Why do I get specified cast is not valid while handling double?
- How to get xml node value by using xelement in c#