私は現在 C# で LINQ を学んでMax()
おり、LINQ ステートメントで関数を使用してオブジェクトを返すより良い方法があるかどうか疑問に思っていました。
ここに私のユーザークラスがあります:
public class User
{
public int ID { get; set; }
public string Name { get; set; }
public double MonthlyWage { get; set; }
}
これは私のテーブル人口クラスです:
public class UsersTable
{
public IList<User> Populate()
{
IList<User> Users = new List<User>()
{
new User{ID = 1, Name = "Bob", MonthlyWage = 1200.00},
new User{ID = 2, Name = "Lee", MonthlyWage = 2200.00},
new User{ID = 3, Name = "Dan", MonthlyWage = 3200.00},
new User{ID = 4, Name = "Liam", MonthlyWage = 4200.00},
new User{ID = 5, Name = "Danny", MonthlyWage = 4213.00},
new User{ID = 6, Name = "Jonathan", MonthlyWage = 1222.00},
new User{ID = 7, Name = "Martin", MonthlyWage = 1233.00},
new User{ID = 8, Name = "Dec", MonthlyWage = 9999.99}
};
return Users;
}
}
これが Main メソッドです。
class Program
{
static void Main(string[] args)
{
UsersTable UserTable = new UsersTable();
IList<User> Users = UserTable.Populate();
double max = Users.Max(x => x.MonthlyWage);
var maxMonthlyWage = Users
.Where(m => m.MonthlyWage == max)
.Select(x => x);
foreach (var item in maxMonthlyWage)
{
Console.WriteLine("{0}: {1} {2} MAX", item.ID, item.Name, item.MonthlyWage);
}
Console.ReadLine();
}
月給が最大のユーザーをdouble max
事前に作成せずに返却する方法はありますか?これは、このタイプのクエリを実行するための最良の方法ですか?