0

我投影以下整数列表,我将得到我的SelectList

    public SelectList ContractOptions = new SelectList(new Proposals().ServiceByContracts.Select(x => (int)x.ContractLength).Distinct().ToList());

我的结果是:

Text Value
0    null
2    null
4    null
1    null

实现这一目标的最佳方法是什么?

我需要:

Text Value
0    0
2    2
4    4
1    1

谢谢!

4

3 回答 3

1
public SelectList ContractOptions = new SelectList(
  new Proposals()
    .ServiceByContracts
    .Select(x => (int)x.ContractLength)
    .Distinct()
    .Select(x=>new{Text=x.ContractLength.ToString(),Value=x.ContractLength.ToString()}),
  "Text","Value");

你也可以试试:

public SelectList ContractOptions = new SelectList(
  new Proposals()
    .ServiceByContracts
    .Select(x => x.ContractLength.ToString())
    .Distinct(),
  "ContractLength","ContractLength");
于 2013-05-30T17:06:29.717 回答
0

试试这个简单的方法

IList<ServiceByContracts> serviceByContractList = new Proposals().ServiceByContracts.Select(x => (int)x.ContractLength).Distinct().ToList();
 public SelectList ContractOptions = new SelectList(serviceByContractList , "YourColumnIdName", "ColoumTextName");

更新

YourColumnIdName意味着您的 ServiceByContracts 表主键 Id 列名称和ColoumTextName您想要的 ServiceByContracts 表文本列名称 ...

于 2013-05-30T16:35:01.563 回答
0
public SelectList ContractOptions = new SelectList(new Proposals().ServiceByContracts.Select(x => (int)x.ContractLength).Distinct().Select( d=>new {Text = d,Value =d}).ToList(),"Text","Value");

好的 .. 我知道 Robert McKee 也有类似的方法,因为这是我自己想出来的。我可以发布我自己的答案!

做个好运动我给了他答案!

于 2013-05-30T17:08:30.417 回答