強く型付けされたクラス、動的オブジェクト、またはタプルを返すことができます。強く型付けされたクラスを返すことを好みます。
タイプを使用する際の問題dynamic
は、実行時にのみインテリセンスと例外を取得しないことです。
タプルの問題は、何を返すかが表示されないことです。あなたや他の開発者は、名前と名前を知るためにメソッドを読む必要があります。
サンプル
public class MyResult
{
public string Name { get; set; }
public string Surname { get; set; }
}
public static MyResult GetAccount(int AccountId)
{
LinqSqlDataContext contextLoad = new LinqSqlDataContext();
var q = (from p in contextLoad.MyAccounts
where p.AccountId == AccountId
select new MyResult{ Name = p.FirstName, Surname = p.Surname }).Single();
return q;
}
アップデート
SingleOrDefault
の代わりに使用することをお勧めしますSingle
。null
これにより、例外をスローする代わりに、アカウントが存在しない場合に結果を確実に取得できます。
//
select new MyResult{ Name = p.FirstName, Surname = p.Surname }).SingleOrDefault();
//