你如何按ICollection的属性排序?

时间:2014-12-27 02:22:17

标签: c# linq icollection

我正在试图找出对ICollection模型参数的显示或LINQ调用。具体来说,这是PurchReq(采购申请)的模型:

public enum FiscalYear
{
    [Display(Name="2013")]
    LastYear,
    [Display(Name="2014")]
    ThisYear,
    [Display(Name="2015")]
    NextYear
}

public class PurchReq
{
    [Key]
    public int PurchReqID { get; set; }
    [Display(Name="Fiscal Year")]
    public FiscalYear FiscalYear { get; set; }
    //[ForeignKey("Project")]
    //public int ProjectID { get; set; }
    [Required]
    [Display(Name="Purchase Requisition Number")]
    public string PurchReqNum { get; set; }
    public string GLCode { get; set; }
    public string Fund { get; set; }
    public string Division { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Purch Req Created")]
    public DateTime? DateCreated { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    [Display(Name = "Purch Req Submitted")]
    public DateTime? DateSubmitted { get; set; }

    //many projects to many purchReqs
    //public virtual Project Project { get; set; }
    public virtual ICollection<Project> Projects { get; set; }

    public PurchReq()
    {
        DateCreated = System.DateTime.Now;
        Projects = new HashSet<Project>();
    }

这是我的PurchReqController索引方法:

  var purchReqs = from p in db.PurchReqs
                        select p;

        if (!string.IsNullOrEmpty(searchString))
        {
            purchReqs = purchReqs.Where(s => s.Projects.Any(item => item.Vendor.VendorName.Contains(searchString)));
        }
        switch (sortOrder)
        {
            case "ProjectName_desc":;
                purchReqs = purchReqs.OrderByDescending(s => s.Projects);
                    //Any(item => item.Vendor.VendorName).ThenBy(n => n.Project.Description));
                break;
            case "PurchReqNum_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.PurchReqNum);
                break;
            case "PurchReqNum_asc":
                purchReqs = purchReqs.OrderBy(s => s.PurchReqNum);
                break;
            case "FiscalYear_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.FiscalYear);
                break;
            case "FiscalYear_asc":
                purchReqs = purchReqs.OrderBy(s => s.FiscalYear);
                break;
            case "GLCode_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.GLCode);
                break;
            case "GLCode_asc":
                 purchReqs = purchReqs.OrderBy(s => s.GLCode);
                break;
            case "Fund_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.Fund);
                break;
            case "Fund_asc":
                purchReqs = purchReqs.OrderBy(s => s.Fund);
                break;
            case "Division_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.Division);
                break;
            case "Division_asc":
                purchReqs = purchReqs.OrderBy(s => s.Division);
                break;
            case "DateCreated_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.DateCreated);
                break;
            case "DateCreated_asc":
                purchReqs = purchReqs.OrderBy(s => s.DateCreated);
                break;
            case "DateSubmitted_desc":
                purchReqs = purchReqs.OrderByDescending(s => s.DateSubmitted);
                break;
            case "DateSubmitted_asc":
                purchReqs = purchReqs.OrderBy(s => s.DateSubmitted);
                break;
            default:
                purchReqs = purchReqs.OrderBy(s => s.Projects);
                //purchReqs = purchReqs.OrderBy(s => s.Projects.Any(item => item.Vendor.VendorName)).ThenBy(n => n.Project.Description);
                break;
        }
        return View(purchReqs);

上面的控制器抛出DbSort错误,因为它无法对ICollection项目进行排序,这是可以理解的。我需要通过Projects.Project.Vendor.VendorName对集合进行排序。这最初适用于PurchReq to Project多对一但最近改为多对多。任何建议和帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

试试这个:

purchReqs = purchReqs.OrderBy(pr => pr.Projects.Count > 0 ? pr.Projects.First().Vendor.VendorName : "")
                     .ThenBy (pr => pr.Projects.Count > 0 ? pr.Projects.First().Description       : "");

我们在这里使用First代替Any;它给了我们一些合适的元素(当然,无论哪个都没关系,但是Any只给了我们一个布尔值,而且没有任何其他特定的东西可以让我们以半随机方式选择一个。)

在C#6中,我认为你可以使用null传播成员访问运算符来做这样的事情:

purchReqs = purchReqs.OrderBy(pr => pr.Projects.FirstOrDefault()?.Vendor?.VendorName)
                     .ThenBy (pr => pr.Projects.FirstOrDefault()?.Description);

无论哪种方式,假设Project具有Description属性,两个排序调用都使用相同的结构。

答案 1 :(得分:1)

我知道我在这个帖子中已经很晚了,并且不是MVC的专家,但就我对订阅集合的理解而言,它通过获取一个值并将其与同一列中的另一个值进行比较来工作。基本上,您需要从所需的集合中选择一个字段并将其提供给OrderBy lambda。

尽可能简单,它有点像这样:

IQueryable purchReq = context.PurchReq.OrderBy(p => p.Projects.Select(pj => pj.Project).FirstOrDefault().Select(v => v.Vendor.VendorName).FirstOrDefault());

如果Project与供应商的关系也是1到n,请替换

Select(v => v.Vendor.VendorName);

通过

Select(v => v.Vendor).FirstOrDefault().VendorName

希望这有帮助。