public class Student
{
public string No;
public string Age;
public string Sex;
public List info;
}
class Info
{
public string Id;
public string Mail;
}
想查询所有的Id,No,Age和Mail的信息,并且按照Id>No>Age排序,请问如何实现
要按照大小排序你的Id,No,Age要全部改int类型,要不字符串比较12会小于2。Select展开下再排序,示例代码如下
using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//有的Id,No,Age和Mail的信息,并且按照Id>No>Age排序,请问如何实现
var list = new List<Student>
{
new Student
{
No="1",
Age="18",
Sex="男",
info=new List<Info>{ new Info { Id = "2", Mail = "abc@163.com" }, new Info { Id = "1", Mail = "efg@163.com" } }
},
new Student
{
No="2",
Age="20",
Sex="女",
info=new List<Info>{ new Info { Id = "2", Mail = "hij@163.com" }, new Info { Id = "7", Mail = "lmn@163.com" } }
}
};
var rs = from stu in list
from i in stu.info
orderby i.Id, stu.No, stu.Age
select new { stu.No, stu.Age, stu.Sex, i.Id, i.Mail };
foreach (var r in rs)
Console.WriteLine($"{r.No}\t{r.Age}\t{r.Sex}\t{r.Id}\t{r.Mail}");
Console.ReadKey();
}
}
public class Student
{
public string No;
public string Age;
public string Sex;
public List <Info> info;
}
public class Info
{
public string Id;
public string Mail;
}
}
楼主可以模仿这个 List stuList= stu.OrderByDescending(s=> new{s.stuNO,s.stuName}).ToList();多个字段排序