LINQ查询独特的职能不DROPDOWNLIST工作 [英] Linq Query Distinct Function do not work with DropdownList
问题描述
我要分配LINQ查询结果DROPDOWNLIST它含有
不同的功能
I want to assign Linq Query result to dropdownlist which contain a Distinct function
我的code: -
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();
问题: - 当我写这篇文章code然后我得到以下错误
Problem :- When I write this code then I get Below Error
错误: - 鲜明的方法不支持的
Error:- The method 'Distinct' is not supported.
我得到 System.NotSupportedException
。
我想地区的一个独特的名称分配给的DropDownList
所以,请帮助我这个问题。
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
简单的变化恩pression类型的IEnumerable< T>
而不是<$的C $ C>的IQueryable&LT; T&GT; ,所以编译器调用 Enumerable.Distinct
而不是 Queryable.Distinct
- 和 Enumerable.Distict
肯定会工作。
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.
这篇关于LINQ查询独特的职能不DROPDOWNLIST工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!