简体   繁体   English

如果Linq查询中的语句

[英]If Statement within Linq Query

I am attempting to sort dropdownlist just exactly how it is entered in the database only if a specific id is selected. 我正在尝试对dropdownlist进行排序,只是在选择了特定的id时才将其输入到数据库中。 Otherwise I want them to sorted in ascending order. 否则我希望它们按升序排序。 I was not sure how to add the final component of not sorting the list when a particular Id is selected. 我不确定如何在选择特定Id时添加不对列表进行排序的最终组件。 Here is where i am so far: 这是我到目前为止的地方:

var items = (from p in _db.Funds
                     where p.DesignationId == id
                     orderby p.Name ascending 
                     select new { p.id, p.Name });
        return items;

You mean something like this? 你的意思是这样的?

var items = 
    (from p in _db.Funds
     where p.DesignationId == id
     select new { p.id, p.Name });
if (id != "some id")
{
    items = items.OrderBy(p => p.Name);
}

return items.ToList();

It would be a solution 这将是一个解决方案

var items = (from p in _db.Funds
                 where p.DesignationId == id
                 orderby p.id == "the id" ? p.Name : null 
                 select new { p.id, p.Name });
return items;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM