尝试使用linq从List中选择第一个值

时间:2017-01-25 06:50:05

标签: c#

我尝试根据第一个下拉列表的选择从列表中选择第一个值 使用Linq"。我已经附上屏幕截图以进一步澄清问题。 C#代码

[HttpPost]
        public ActionResult Escalation(FormCollection _frm)
        {
            ErpCode = Session["ErpCode"].ToString();
            List<IssueEL> _issue = new List<IssueEL>();
            IEnumerable<IssueEL> empdetail = new List<IssueEL>();
            try
            {
                if (_frm["Type"].ToString() == "BindIssue")
                {
                    ViewData["Dept"] = _frm["ddlDepartment"];
                    _issue = _dam.Issues.ToList();
                    _issue = (from n in _issue where n.DepartmentId == _frm["ddlDepartment"].ToString() select n).ToList();
                    ViewBag.IssueType = _issue.ToList();                    
                    empdetail = _dam.Issues.ToList();
                    empdetail = empdetail.GroupBy(x => new { x.Employee}).Select(x => x.First());
                    ViewBag.Employee = empdetail;
                    ViewData["Issue"] = "";
                    ViewData["Emp"] = "";
                }
                List<Department> _dep = _dam.Departments.ToList();
                ViewBag.Department = _dep.ToList();
            }
            catch (Exception ex)
            {
                _apm.GetException(ex, "GetFeeds", "EmployeeController");
            }
            return View();
        }

cs Html

@using (Html.BeginForm("Escalation", "Employee", FormMethod.Post, new { @class = "IssueForm" }))
        {
            <div class="col-md-12 text">
                <div class="col-md-4">
                    Department
                </div>
                <div class="col-md-8">
                    @Html.DropDownList("ddlDepartment", new SelectList(ViewBag.Department, "DepartmentId", "DepartmentName", ViewData["Dept"]), "-- " +
             HttpContext.GetLocalResourceObject(_vpth, "Department", _owrpr.GlobalCultureInfo).ToString() + " --", new { @class = "form-control"})
                </div>
                <div class="col-md-4">
                    Issue Type
                </div>
                <div class="col-md-8">
                    @Html.DropDownList("ddlIssue", new SelectList(ViewBag.IssueType, "IssueId", "IssueType", ViewData["Issue"]), "-- Select Issue --", new { @class = "form-control" })
                </div>
                <div class="col-md-4">
                    Employee
                </div>
                <div class="col-md-8">
                    @Html.DropDownList("ddlEmploy", new SelectList(ViewBag.Employee, "EmployeeId", "Employee", ViewData["Empl"]), "-- " +
             HttpContext.GetLocalResourceObject(_vpth, "Employ", _owrpr.GlobalCultureInfo).ToString() + " --", new { @class = "form-control" })
                </div>
                <div class="col-md-4">
                    Priority
                </div>

详情以图片enter image description here

提供

5 个答案:

答案 0 :(得分:0)

使用FirstOrDefault

ViewBag.Department = _dep.FirstOrDefault();

答案 1 :(得分:0)

你不能这样做吗?

string dep = _frm['ddlDepartment'].ToString();
Issue tmp=dbContext.Issues.where(x=>x.DepartmentId==dep).FirstOrDefault();
ViewBag.TMP = tmp;

然后在你看来

@Html.DropDownListFor(m => m.EmployeeName,new SelectList(ViewBag.TMP,"EmployeeName", "EmployeeName"))

答案 2 :(得分:0)

您可以在LINQ中使用FirstOrDefault()。这将基本返回列表的第一个值,如果列表为空,则返回默认值

List<Department> _dep = _dam.Departments.ToList();
ViewBag.Department = _dep.FirstOrDefault();

答案 3 :(得分:0)

不太清楚你的意思,但这里有一个可以用于所有Linq的合理解释。

Linq适用于所有Enumerable,IEnumerable,IEnumberable&lt; T&gt;和IQueryable&lt; T>对象类型...如果你引用了linq&#39;使用System.Linq&#39;那么你可以或应该能够查询任何这些对象类型,如数组,列表等。

所以,要获得任何这些类型的第一项,你只需要......

var first = someEnumeratedObject.First();

现在,如果没有第一个对象,那么您将收到错误。如果你想在没有任何东西可以首先返回时抛出错误,只能使用.First()。

最流行且可能最好的选择是使用FirstOrDefault(),如果该对象不存在,将返回默认值(T)。

var first = someEnumaratedObject.FirstOrDefault();

有一个你经常不关心但应该注意的问题。当使用FirstOrDefault()时,编译的返回类型default(T)意味着如果它是非可空类型,您将返回默认值。所以要小心。

一个例子是整数列表。如果您的第一个值为0,那么FirstOrDefault()将返回0 ...如果您的列表为空,那么您的FirstOrDefault()仍将返回0.有时您需要确定要谨慎选择。

    var list = new List<int> { 1, 2, 3 };
    var list2 = new List<int>();
    Console.WriteLine(list.First()); //Prints 1
    //Console.WriteLine(list2.First());  <- Throws an error
    Console.WriteLine(list.FirstOrDefault()); //Prints 1
    Console.WriteLine(list2.FirstOrDefault()); //Prints 0

    Console.Read();

答案 4 :(得分:0)

你可以试试!!

var firstItemsInGroup = from b in empdetail
             group X by X.Employee into g select g.First();