HOW CAN I CONVERT IENUMERABLE<T> TO LIST<T> IN C#?

ben reitman

Total Post:96

Points:676
Posted by  ben reitman
C# 
C#
 2259  View(s)
Ratings:
Rate this:

I am using LINQ to query a generic dictionary and then use the result as the datasource for my ListView.

Dictionary<Guid, Record> dict = GetAllRecords();

List<Record> searchResults = new List<Record>();

var matches = dict.Values.Where(rec => rec.Name == "foo");

foreach (Record rec in matches)

    searchResults.Add(rec);

myListView.DataSource = searchResults;

myListView.DataBind();

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How can I convert IEnumerable<T> to List<T> in C#?

    Hi Ben,

    Try this:

    var matches = dict.Values.Where(rec => rec.Name == "foo").ToList();


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!