这可能是一个小问题,但我真的无法找到它发生的位置。
我有一个包含三列的表,其名称为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;
}
第一个,this.ViewData [&#34; country&#34;]工作正常,创建一个这样的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 VandenHeuvel在Populating a SelectList from a DataTable中的答案
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");
答案 0 :(得分:0)
您可以像这样更改查询:
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();