我可以在Linq中使用OrderBy()。Then()對具有重複值的實體的字符串屬性。 爲例Linq OrderBy然後重複項
public class Test
{
public Guid Id{get;set;}
public string Name{get;set;}
public DateTime Date{get;set;}
}
我要訂購基於名稱測試實體列表(即可以複製),然後,命令日期的結果。
例如
public class Test
{
public Guid Id { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
}
public class TestLinq
{
private IList<Test> list;
public TestLinq()
{
var dateTime = DateTime.Now;
list = new List<Test>
{
new Test {Id = Guid.NewGuid(), Name = "Masoud", Date = dateTime},
new Test {Id = Guid.NewGuid(), Name = "Bahrami", Date = dateTime},
new Test {Id = Guid.NewGuid(), Name = "Ali", Date = DateTime.Now},
new Test {Id = Guid.NewGuid(), Name = "hasan", Date = DateTime.Now},
new Test {Id = Guid.NewGuid(), Name = "Masoud", Date = DateTime.Now},
new Test {Id = Guid.NewGuid(), Name = "Bahrami", Date = DateTime.Now},
};
}
public List<Test> Get(string name)
{
return list.OrderBy(test => test.Name).ThenBy(test => test.Date).ToList();
}
}
是的,你可以,你面臨的問題是什麼? –
@ Rahul Singh,當運行查詢我發生了這個錯誤「一個列已被指定不止一次按順序排列。order by必須是唯一的」 –
@MasoudBahrami你可以發佈整個查詢,我懷疑你指定了Name列按子句排序兩次? –