我正在尝试使用linq将我的枚举绑定到下拉列表
public enum InvoiceTemplateHeader
{
TENANTNAME,
TENANTADDRESS,
TENANTCITY,
TENANTSTATE,
TENANTCOUNTRY,
SONO,
SODATE,
SOAMOUNT,
SOSTATUS,
EXP_DELDATE,
INVOICENO,
INVOICEDATE,
INVOICEAMOUNT,
NETAMOUNT,
HANDLINGCHARGE,
PAIDAMOUNT,
INVOCIEQTY,
TAXPERCENT,
TAXAMOUNT,
TAXDETAILVIEW,
DISCOUNTPERC,
DISCOUNTAMOUNT,
BILLINGADDRESS,
RETAILERNAME,
RETAILERADDRESS,
DELIVERYADDRESS,
TERMANDCONDITIONS,
SIGINATURE,
INVOICEDETAIL,
AMOUNTINWORDS,
LineItem,
HeaderData,
FooterData
}
我的方法:
private List<SelectListItem> FillInvoiceHeaderTemplate()
{
List<String> fields = null;
fields = _RenderingTextboxFieldsValues.Split(ServiceDataManager.CommaSplitter).ToList();
return (new List<SelectListItem>() { new SelectListItem() { Text = Misc.getResource("APP_CMN_LBL_OptionSelect"), Value = "" } }).Union
(Enum.GetValues(typeof(ServiceDataManager.InvoiceTemplateHeader)).Cast<ServiceDataManager.InvoiceTemplateHeader>().Select(v => new SelectListItem
{
Text = v.ToString(),
Value = v.ToString()
})).ToList();
}
我获得了所有枚举值。 我希望通过某些值过滤掉它,所以我硬编码了一个字符串。
private string _RenderingTextboxFieldsValues =
"TENANTNAME[*],TENANTADDRESS[*],TENANTCITY,TENANTSTATE[*]";
我只想将枚举中的这些项目绑定。
答案 0 :(得分:0)
为什么不创建仅包含所需值的SelectList
:
private List<SelectListItem> FillInvoiceHeaderTemplate()
{
return new List<SelectListItem>
{
new SelectListItem { Text = Misc.getResource("APP_CMN_LBL_OptionSelect"), Value = "" }
new SelectListItem { Text = InvoiceTemplateHeader.TENANTNAME.ToString(),Value = InvoiceTemplateHeader.TENANTNAME.ToString() },
new SelectListItem { Text = InvoiceTemplateHeader.TENANTADDRESS.ToString(),Value = InvoiceTemplateHeader.TENANTADDRESS.ToString() },
new SelectListItem { Text = InvoiceTemplateHeader.TENANTCITY.ToString(),Value = InvoiceTemplateHeader.TENANTCITY.ToString() },
new SelectListItem { Text = InvoiceTemplateHeader.TENANTSTATE.ToString(),Value = InvoiceTemplateHeader.TENANTSTATE.ToString() }
}
}
但是,如果你仍然喜欢使用linq和过滤,那么只有在_RenderingTextboxFieldsValues
中添加选项列表项时,才能在.Where(v=> fields.Contains(v.ToString()))
之前添加类似Select
的内容:
private string _RenderingTextboxFieldsValues = "TENANTNAME,TENANTADDRESS,TENANTCITY,TENANTSTATE";
private List<SelectListItem> FillInvoiceHeaderTemplate()
{
List<String> fields = null;
fields = _RenderingTextboxFieldsValues.Split(ServiceDataManager.CommaSplitter).ToList();
return (new List<SelectListItem>() { new SelectListItem() { Text = Misc.getResource("APP_CMN_LBL_OptionSelect"), Value = "" } }).Union
(Enum.GetValues(typeof(ServiceDataManager.InvoiceTemplateHeader)).Cast<ServiceDataManager.InvoiceTemplateHeader>().Where(v=> fields.Contains(v.ToString())).Select(v => new SelectListItem
{
Text = v.ToString(),
Value = v.ToString()
})).ToList();
}