I want to assign Linq Query result to dropdownlist which contain a Distinct function
My Code:-
var area = de.City_Area_View
.Select(m => new { m.Area_Id, m.Area_Name})
.Distinct()
.ToList();
drpfilter.DataTextField = "Area_Name";
drpfilter.DataValueField = "Area_Id";
drpfilter.DataSource = area;
drpfilter.DataBind();
Problem :- When I write this code then I get Below Error
Error:- The method 'Distinct' is not supported.
I get System.NotSupportedException
.
I want to assign a Distinct name of area to the DropDownList So please help me for this problem.
If your set is small enough (so you don't mind fetching all the values from the database), the simplest thing would be to force the distinct part to be performed locally:
var area = de.City_Area_View
.Select(m => new { m.Area_Id, m.Area_Name})
.AsEnumerable()
.Distinct()
.ToList();
AsEnumerable
simply "changes" the expression type to IEnumerable<T>
instead of IQueryable<T>
, so that the compiler calls Enumerable.Distinct
instead of Queryable.Distinct
- and Enumerable.Distict
will definitely work.
See more on this question at Stackoverflow