2

次のクラスで FirstName と Surname を返すにはどうすればよいですか?

public static string GetAccount(int AccountId)
{        
    LinqSqlDataContext contextLoad = new LinqSqlDataContext();

    var q = (from p in contextLoad.MyAccounts
             where p.AccountId == AccountId
             select new { Name = p.FirstName, Surname = p.Surname }).Single();

    return ??;
} 
4

7 に答える 7

6

強く型付けされたクラス、動的オブジェクト、またはタプルを返すことができます。強く型付けされたクラスを返すことを好みます。

タイプを使用する際の問題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の代わりに使用することをお勧めしますSinglenullこれにより、例外をスローする代わりに、アカウントが存在しない場合に結果を確実に取得できます。

//
select new MyResult{ Name = p.FirstName, Surname = p.Surname }).SingleOrDefault();
//
于 2012-07-26T11:44:07.073 に答える
4

戻り値の型に新しいオブジェクトを定義したくない場合は、Tuple<string, string>.

于 2012-07-26T11:43:53.217 に答える
1

参照によって 2 つのオブジェクトを渡すと、それらを設定するだけです。

コード臭の少ないバージョンの例として、try関数にするように変更

public static bool TryGetAccount(int AccountId, out String FirstName, out String Surname)
{        
    LinqSqlDataContext contextLoad = new LinqSqlDataContext();

    var q = (from p in contextLoad.MyAccounts
             where p.AccountId == AccountId
             select new { Name = p.FirstName, Surname = p.Surname }).SingleOrDefault();


    FirstName=(q==null) ? null: q.Name;
    Surname=(q==null) ? null: q.Surname;
    return q!=null;
} 

今、あなたはすることができます

string firstName;
string surname;

if (TryGetAccount(id, out firstName,out surname)) {
  // firstName now equals the first name and surname now equals the surname
} else {
  // Deal with value not found

}

于 2012-07-26T11:51:02.247 に答える
1

さらに別の(最善ではない:))オプションは、配列を返すことです:

public static string[] GetAccount(int AccountId)
{        
    LinqSqlDataContext contextLoad = new LinqSqlDataContext();

    var q = (from p in contextLoad.MyAccounts
             where p.AccountId == AccountId
             select new { Name = p.FirstName, Surname = p.Surname }).Single();

    return new []{q.Name, q.Surname};
} 
于 2012-07-26T11:52:22.070 に答える
0

.Net 4を使用している場合は、文字列の代わりに動的を返し、返されたオブジェクトから両方の値を直接取得できます。

于 2012-07-26T11:44:22.513 に答える
0

Hashtableを使用して、新しい結果クラスの作成を回避できます。このようなもの:

  public static Hashtable GetAccount(int AccountId)
    {        
       LinqSqlDataContext contextLoad = new LinqSqlDataContext();

         var q = (from p in contextLoad.MyAccounts
         where p.AccountId == AccountId
         select new { Name = p.FirstName, Surname = p.Surname }).Single();

        return new Hashtable(q.FirstName, q.Surname);
    } 

あなたがキーとしてあなたのFirstNameによってSurnameを得ることができるより。

于 2012-07-26T13:40:48.577 に答える
0

tuple.Item1, tuple.Item2 のように返された型を使用することを気にしない限り、タプルとして返すのはどうですか

于 2012-07-26T11:50:44.937 に答える