【问题标题】:Where condition in Razor DropdownList MVCRazor DropdownList MVC 中的 where 条件
【发布时间】:2013-07-30 08:39:33
【问题描述】:

我正在 MVC 中生成一个下拉列表。我有一个模型,它有两个列表值。我的代码看起来像

@Html.DropDownList("Test", new SelectList(
                            Model.Test.Where(i=>i.Id = *from other list*), "Id", "Name"))

在我的模型中,我有两个列表: List<Test>List<Test2>。我正在尝试生成 Test1.Id 等于 Test2.Id 的下拉列表。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 linq razor lambda


    【解决方案1】:

    试试这个:

    @Html.DropDownList("Test", new SelectList(
         Model.Test.Where(i=>Model.Test2.Any(ii=> i.Id == ii.Id)), "Id", "Name"))
    

    Any 语句示例:

        static void Main(string[] args)
        {
            List<string> listA = new List<string>() { "A", "B", "C" };
            List<string> listB = new List<string>() { "D", "F", "A", "B", "E" };
    
            var result = listB.Where(lb => listA.Any(la => la == lb));
            Console.WriteLine(result.Count());
        }
    

    结果:2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多