【问题标题】:Sorting array with multiple column in C#在 C# 中对具有多列的数组进行排序
【发布时间】:2016-11-22 04:07:20
【问题描述】:

C#代码

方法

[System.Web.Services.WebMethod]
    public static SubjectList[] GetStudentList(string location, string program, long batch, string term,string code ,string name)
{
    using (Entities obj = new Entities())
    {
        SubjectListStatus subObjStatus = new SubjectListStatus();
        List<string> rollno = obj.Student_Master.Where(i =>i.Student_Location==location && i.Student_Course==program && i.Student_Batch==batch).Select(i => i.Student_RollNo).ToList();
        foreach (var rollObj in rollno)
        {
            var listLoc = obj.Marks_Master.Where(k => k.Location == location && k.Course_ID == program && k.Subject_ID == code && k.Term_ID == term && k.Student_ID.Equals(rollObj)).ToList();
            foreach (var tempobj in listLoc)
            {
                SubjectList subObj = new SubjectList();
                subObj.Subject_Code = tempobj.Student_ID;                   
                subObj.Total_Obtained = tempobj.Marks_Obtained; ;
                subObj.Total_Outoff = tempobj.Marks_Total; ;
                subObjStatus.Add(subObj);
            }
        }
       //sort Here
        return subObjStatus.ToArray();
    }
}

public class SubjectList
{
    public string Subject_Code { get; set; }
    public string Subject_Name { get; set; }      
    public long Total_Obtained { get; set; }
    public long Total_Outoff { get; set; }  
}
public class SubjectListStatus : List<SubjectList>
{
    public void Add(SubjectList st)
    {
        base.Add(st);
    }

}

我想在返回数据之前对列Total_Obtained 进行降序排序 我试过Array.Sort 但显示以下错误

1)如何使用Array.Sort

2) 任何其他简单的方法来进行这种排序

我把图像作为数据

提前致谢

【问题讨论】:

  • 之后为什么不使用 LINQ OrderBy 和 ToArray?
  • 我想对 Total_Obtained 进行排序,它是 Marks_Master 表的一部分,我试过像 .OrderByDescending(i => i.Marks_Obtained) 但不起作用
  • 我找不到你在哪里使用这种排序
  • 在返回数据之前看到我把评论像 //sort Here

标签: c# entity-framework sorting webmethod


【解决方案1】:

OrderBy,就像所有 LINQ 扩展方法一样,不会修改对象,它只返回一个您需要存储的序列(或直接返回它们)。所以在你的代码中,

 return subObjStatus.OrderByDescending(x=>x.Total_Obtained).ToArray();

应该可以。另外,我有几个建议:

[System.Web.Services.WebMethod]
    //change to IEnumerable instead of array
    public static IEnumerable<SubjectList> GetStudentList(string location, string program, long batch, string term,string code ,string name)
{
    using (Entities obj = new Entities())
    {
        //If all subObjStatus did is keeping the SubjectList, you don't really need it
        //SubjectListStatus subObjStatus = new SubjectListStatus();
        //Don't store it into list, just keep it lazy loaded
        var rollno = obj.Student_Master.Where(i =>i.Student_Location==location && i.Student_Course==program && i.Student_Batch==batch).Select(i => i.Student_RollNo);
        foreach (var rollObj in rollno)
        {
            //same thing here, keep it lazy loaded
            var listLoc = obj.Marks_Master.Where(k => k.Location == location && k.Course_ID == program && k.Subject_ID == code && k.Term_ID == term && k.Student_ID.Equals(rollObj));
            foreach (var tempobj in listLoc)
            {
                SubjectList subObj = new SubjectList();
                subObj.Subject_Code = tempobj.Student_ID;                   
                subObj.Total_Obtained = tempobj.Marks_Obtained; ;
                subObj.Total_Outoff = tempobj.Marks_Total;
                subObj.Subject_Name = //you probably want the name too 
                yield return subObj;
            }
        }

    }
}

如果您不熟悉,请阅读有关yield 的更多信息。

GetStudentList 的调用者可以枚举结果,或者如果你真的需要它作为数组调用 ToArray。

【讨论】:

  • 感谢你的代码帮助我减少我的代码,现在它只有几行但排序工作放在 rollno.Contains(k.Student_ID) 而不是 foreach 上
【解决方案2】:

在您的排序类型中,subObjStatus 仍然是 SubjectListStatus 类型,如果您坚持这样做,您可以这样做

return Array.Sort(subObjStatus.ToArray());

这样你的SubjectList 就必须实现IComparable 接口。或者你可以使用:

return Array.Sort(subObjStatus.ToArray(),//Custom IComparer);

您可以查看更多参考信息

Array.Sort Documentation

否则你可以使用 LINQ 来做到这一点:

subObjStatus.OrderBy(x => x.[variableToSort]);

然后返回。我认为这是更好的方法。

【讨论】:

  • 我尝试查看有问题的图像,但使用委托时出现转换错误,第二种方式没有任何错误但排序不起作用
  • 关于图片我看不到项目的属性所以我不明白排序
  • 感谢回复我得到了一些不同的解决方案,而不是使用 foreach 我在 where 使用 rollno.Contains(k.Student_ID) 然后使用 .OrderByDescending(i => i.Marks_Obtained) 再次感谢跨度>
【解决方案3】:
    [System.Web.Services.WebMethod]
    public static SubjectList[] GetStudentList(string location, string program, long batch, string term,string code ,string name)
{
    using (Entities obj = new Entities())
    {
        SubjectListStatus subObjStatus = new SubjectListStatus();
        List<string> rollno = obj.Student_Master.Where(i =>i.Student_Location==location && i.Student_Course==program && i.Student_Batch==batch).Select(i => i.Student_RollNo).ToList();
        foreach (var rollObj in rollno)
        {
            var listLoc = obj.Marks_Master.Where(k => k.Location == location && k.Course_ID == program && k.Subject_ID == code && k.Term_ID == term && k.Student_ID.Equals(rollObj)).ToList();
            foreach (var tempobj in listLoc)
            {
                SubjectList subObj = new SubjectList();
                subObj.Subject_Code = tempobj.Student_ID;                   
                subObj.Total_Obtained = tempobj.Marks_Obtained; ;
                subObj.Total_Outoff = tempobj.Marks_Total; ;
                subObjStatus.Add(subObj);
            }
        }
        //return subObjStatus.ToArray();
    // use the below statement to sort by total_obtained in the descending order
       return subObjStatus.OrderByDescending(x=>x.Total_Obtained).ToArray();
    }
}

【讨论】:

    猜你喜欢
    • 2020-03-20
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2016-05-03
    • 2019-12-26
    • 2020-04-01
    相关资源
    最近更新 更多