gpt4 book ai didi

c# - 如何从 LINQ to Entities 获取唯一值?

转载 作者:太空宇宙 更新时间:2023-11-03 21:15:36 25 4
gpt4 key购买 nike

我有一个 DropDownList,它从这个查询中获取数据:

    using (ProjectEntities myEntities = new ProjectEntities ())
{
var q = (from c in myEntities.Customers orderby c.Name select c.Name);
DropDownList1.DataSource = q.ToList();
DropDownList1.DataBind();
}

如何只获取唯一的名称?

最佳答案

只需添加 Distinct

 var q = (from c in myEntities.Customers orderby c.Name select c.Name);
DropDownList1.DataSource = q.ToList().Distinct();

或查询本身,

 var q = (from c in myEntities.Customers orderby c.Name select c.Name).Distinct();

关于c# - 如何从 LINQ to Entities 获取唯一值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com