C#中的DropDownList只获取数据库中第一个不同的行值
本文关键字:第一个 DropDownList 中的 获取 数据库 | 更新日期: 2023-09-27 17:58:40
这可能是一个小问题,但我真的不知道它发生在哪里。
我有一个由三列组成的表,名称为TaxRankID、TaxObjectID和TaxName。控制器中的代码如下:
public ActionResult DefineFauna()
{
var cExtant = PredicateBuilder.True<FaunaPoliticalUnits>();
cExtant = cExtant.And(r => r.PoliticalUnitLevel == 1);
var results = from r in lw.PoliticalUnits.AsExpandable().Where(cExtant.Expand()).OrderBy(r => r.PoliticalUnitName)
select r;
var selectList = new SelectList(results.ToList(), "GeogID", "PoliticalUnitName",5);
this.ViewData["country"] = selectList;
this.ViewData["tscope"] = ListHelp();
return View();
}
private SelectList ListHelp()
{
var tsExtant = PredicateBuilder.True<FaunaTaxonomicScopes>();
tsExtant = tsExtant.And(r => r.TaxRankID == 18 || r.TaxRankID == 17 || r.TaxRankID == 24);
var tsresults = from r in lw.TaxonomicScopes.AsExpandable().Where(tsExtant.Expand()).OrderBy(r => r.TaxRankID).ThenBy(r => r.TaxName)
select r;
var tsselectList = new SelectList(tsresults.ToList(), "TaxObjectID", "TaxName");
return tsselectList;
}
第一个,这个。ViewData["country"]运行良好,创建了这样的DropDownList:
<select id="country" name="country" style="width: 300;">
<option value="162">Afghanistan</option>
<option value="103">Albania</option>
<option value="82">Algeria</option>
<option value="4118">Andorra</option>
<option value="157">Angola</option>
第二个,我甚至把它作为一个单独的函数移走了,看起来和第一个完全一样,但产生了奇怪的结果。
<select id="tscope" name="tscope" style="width: 300;"><option value="12996">Neuropterida (all)</option>
<option value="12933">Glosselytrodea only</option>
<option value="12933">Glosselytrodea only</option>
<option value="12933">Glosselytrodea only</option>
<option value="12933">Glosselytrodea only</option>
<option value="12933">Glosselytrodea only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
<option value="12790">Glosselytrodea : Archoglossopteridae only</option>
</select>
正如你所看到的,这些都是一样的。但我确信在数据库中它们是不一样的,
TaxRankID TaxObjectID TaxName
17 12996 Neuropterida (all)
18 12933 Glosselytrodea only
18 12922 Megaloptera only
18 12921 Neuroptera only
18 17137 Neuropterida, incertae sedis only
18 12923 Raphidioptera only
24 12790 Glosselytrodea : Archoglossopteridae only
24 12791 Glosselytrodea : Glosselytridae only
24 12380 Glosselytrodea : Jurinidae only
24 12262 Glosselytrodea : Permoberothidae only
24 12234 Megaloptera : Corydalidae only
24 12738 Megaloptera : Corydasialidae only
24 12280 Megaloptera : Euchauliodidae only
24 12911 Megaloptera : Nanosialidae only
24 12290 Megaloptera : Parasialidae only
24 12237 Megaloptera : Sialidae only
24 12811 Neuroptera : Aetheogrammatidae only
看起来像这样。很明显,它只是选择TaxRankID的第一个值,这是不同的。
你知道为什么会发生这种事吗?
感谢您的帮助。
我通过使用纯SQL和DataTable成功地生成了正确的DropDownList,但我仍然很好奇为什么原始的会生成奇怪的结果。
如果有人需要,修改后的代码粘贴在下面,这要归功于Ryan VandenHubel在从DataTable 填充SelectList中的回答
DataTable subjects = new DataTable();
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter adapter = new SqlDataAdapter("SELECT TaxRankID, TaxObjectID, TaxName FROM lacewing.dbo.tblLdlFaunaTaxonomicScopes ORDER BY TaxRankID, TaxName", con);
adapter.Fill(subjects);
List<SelectListItem> list = new List<SelectListItem>();
foreach (DataRow row in subjects.Rows)
{
list.Add(new SelectListItem()
{
Text = row[2].ToString(),
Value = row[1].ToString()
});
}
return new SelectList(list, "Value", "Text");
您可以这样更改查询:
var ids=new List<int> {17,18,24};
var results = lw.TaxonomicScopes.Join(ids,
x => x.TaxRankID ,
y => y.TaxRankID ,
(x, y) => x)
.OrderBy(r => r.TaxRankID)
.ThenBy(r => r.TaxName)
.ToList();