I’m using entity framework 5 in ASP.NET MVC 4 and I’m new to it. I want to get an entities primary keys as a list. Primary key is of type Guid. When I write the following code I get error:
List<Guid> list = DB.Complex.Where(d => d.IsActive == true)
.Select(d => d.PKComplexRoomID).ToList<Guid>();
Error:
'System.Linq.IQueryable<System.Guid?>' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)' has some invalid arguments
I googled it but could not find anything.
It looks like
PKComplexRoomIDis nullable, since your select result is of typeSystem.Linq.IQueryable<System.Guid?>.It depends on your situation, but assuming room ID won’t be null for active entities, you can try:
List<Guid> list = DB.Complex.Where(d => d.IsActive == true && d.PKComplexRoomID.HasValue).Select(d => d.PKComplexRoomID.Value).ToList();