I am trying to formulate a LINQ query to select a sublist of a list where it meets a where condition like so:
List<Entities.Base> bases = this.GetAllBases();
List<Entities.Base> thebases = from aBase in bases
where aBase.OfficeCD == officeCD
select aBase;
where Base is just an Entity class:
public string BaseCD { get; set; }
public string BaseName { get; set; }
public string OfficeCD { get; set; }
public DateTime EffectiveDate { get; set; }
public DateTime ExpirationDate { get; set; }
I am getting an error “Cannot implictly convert type System.Collections.Generic.IEnumerable to System.Collections.Generic.List
So I tried to apply the Cast operator but that fails. I see now that I am not tring to convert the type of the element. How can I solve this issue? Thanks!
This is not really a problem that can be solved by “casting”; the result of the query you’ve got isn’t a list – it’s a deferred-executing sequence that will stream the matching items out on demand. You will have to actually load these results into a
List<T>to achieve your purpose. For example, theEnumerable.ToListmethod will create a new list, populate it with the results of the query and then return it.A few options: