Cannot implicitly convert type 'System.Linq.IQueryable<TMS.Models.CustomAsset>' to 'System.Collections.Generic.ICollection

asp.net-mvc, asp.net-mvc-4, c#

Solution

What you have stored in the `customerAssets` is just a query - a way, how to get the data. It's not the data itself yet, because it's lazily evaluated. `ICollection<T>` is an interface built for manipulating data collections that you already have. The query does not implement it, so you cannot implicitly convert from `IQueryable<T>` to the `ICollection<T>` Calling `ToList()` is a simple way how to force loading the data into an `ICollection<T>`, but it also means in your case, that at that place in code (and execution time) the query will get executed and data will be loaded from whatever database you are querying.

Problem

I have the following model class :- ``` public class CustomerCustomAssetJoin { public CustomAsset CustomAsset { get; set; } public ICollection<CustomAsset> CustomAssets { get; set; } } ``` But when i wrote the following method:- ``` public CustomerCustomAssetJoin CustomerCustomAsset(string customerName) { var customerAssets = tms.CustomAssets.Include(a => a.CustomAssetType).Where(a => a.CustomerName.ToLower() == customerName.ToLower()); CustomerCustomAssetJoin caj = new CustomerCustomAssetJoin { CustomAsset = new CustomAsset {CustomerName = customerName }, CustomAssets = customerAssets }; return caj; } ``` I got the following exception : Error 20 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.ICollection'. An explicit conversion exists (are you missing a cast?) So what is causing this error? To overcome this error i just add a .toList() as follows: ``` var customerAssets = tms.CustomAssets.Include(a => a.CustomAssetType).Where(a => a.CustomerName.ToLower() == customerName.ToLower()); ``` So why do I have to convert it to a list?

Original source

Related problems