我遇到了从linq查询中填充下拉菜单的问题。这里是我的代码片段:使用SelectList从linq中获取文本到下拉菜单时出现错误
theList = new SelectList(from e in db.MyTab
join f in db.MyOtherTab on e.TypeId equals MyOtherTab.TypeId
select new
{
Text = e.Name + " "
+ e.Code + " "
+ f.TypeDescription + " "
+ e.Class + " "
+ e.Series + " "
+ SqlFunctions.StringConvert(e.InterestRate),
Value = e.Id
}, "Value", "Text");
两个e.Name
和e.Code
是在我的表varchar(50)
。 InterestRate
是decimal
,因此SqlFunctions.StringConvert
功能。
Name
,Code
,Class
,Series
和TypeDescription
是我的表varchar
类型。
这工作如果Name
,Code
,Class
,Series
和TypeDescription
是文字或字母数字。问题是如果Name
,Code
,Class
和Series
是数字(尽管类型为varchar
),则填充SelectList
。
如果是这种情况,则下拉列表中有一个空行。
我不明白这是为什么。如果它是一个varchar,它应该无关紧要,如果它是“123”,它仍然是一个varchar,对吧?也许这不是问题,但是,检查这些表,如果ame,Code,Class和Series是数字,则下拉列中的行为空白是一致的。
有谁知道我该如何解决这个问题?
谢谢。
有没有规定说你必须在一个声明中做的一切 – Jonesopolis