使用linq填充包含单词的列表

时间:2016-02-24 09:18:52

标签: c# asp.net-mvc linq

这是我的清单:

List<ActionType> actionTypeList = new ActionTypeLogic(ApplicationType.Web).GetAllActionTypes();

我想用actionTypeList只有使用Linq的单词(完整,新的,仍然)来填充下拉列表

3 个答案:

答案 0 :(得分:1)

你的意思是

string[] words = new[] {"complete", "new", "still"};
actionTypeList.Where(x => words.Contains(x.Word)).ForEach(x => myDropDown.Items.Add(new ListItem(x.Word, x.Value)));

List.ForEach method不是真正的LINQ,但它适合这种情况。

否则,我恳请您更详细地说明您的问题。

答案 1 :(得分:1)

这个怎么样?

 new ActionTypeLogic(ApplicationType.Web).GetAllActionTypes().Select(x => x.MyWord).ToList();

答案 2 :(得分:1)

您的代码应该是这样的:

string[] words = new []{"new", "complete", "still"}; 
dropDownList.DataSource = actionTypeList.Where(at=>words.Contains(at.SomeField)).Select(at=> new {at.SomeField, at.SomeOtherField});
dropDownList.DataTextField = "some title for first field";
dropDownList.DataValueField = "some title for second field";
dropDownList.DataBind();