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!
In C#, an IEnumerable can be converted to a List through the following lines of code: IEnumerable enumerable = Enumerable. Range(1, 300); List asList = enumerable. ToList();
The main difference between IEnumerable and List in C# is that IEnumerable is an interface, while List is a concrete class. Moreover, IEnumerable is read-only and List is not.
This extension method converts collections (IEnumerables) to List instances. It is fast and easy-to-remember. It returns a List instance with the appropriate elements.
IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.
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 aList<T>
to achieve your purpose. For example, the Enumerable.ToList
method will create a new list, populate it with the results of the query and then return it.
A few options:
var thebases = (from aBase in bases
where aBase.OfficeCD == officeCD
select aBase).ToList();
// fluent syntax
var thebases = bases.Where(aBase => aBase.OfficeCD == officeCD)
.ToList();
// not a LINQ method - an instance method on List<T>.
// Executes immediately - returns a List<T> rather than a lazy sequence
var thebases = bases.FindAll(aBase => aBase.OfficeCD == officeCD);
// "manual" ToList()
var theBases = new List<Entities.Base>();
var matchingBases = from aBase in bases
where aBase.OfficeCD == officeCD
select aBase;
foreach(var matchingBase in matchingBases)
theBases.Add(matchingBase);
In addition to the method @Ani mentioned, you can also use LINQ to select the data directly into your classes like this:
List<Entities.Base> bases = this.GetAllBases();
List<Entities.Base> thebases = new List<Entities.Base>(
from aBase in bases
where aBase.OfficeCD == officeCD
select new Entities.Base {
BaseCD = aBase.BaseCD,
BaseName = aBase.BaseName,
OfficeCD = aBase.OfficeCD,
EffectiveDate = aBase.EffectiveDate,
ExpirationDate = aBase.ExpirationDate
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With