Gurizao
Gurizao

Reputation: 35

return multiple values in static class

How do I return FirstName and Surname in the following class?

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 ??;
} 

Upvotes: 2

Views: 2979

Answers (7)

Phist
Phist

Reputation: 68

You can use the Hashtable to evade creating the new result-class. Something like this:

  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);
    } 

Than you can get the Surname by your FirstName as key.

Upvotes: 0

Joshua
Joshua

Reputation: 552

Pass in two objects by reference and you can just set them.

Altered to make it a try function as an example of a less code smell version

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;
} 

Now you can do

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

}

Upvotes: 1

dknaack
dknaack

Reputation: 60556

You can return a strongly typed class, dynamic object or a tuple. I prefer to return a strongly typed class.

The problem using the dynamic type is that you dont get intellisense and exceptions only at runtime.

The problem with a tuple is that it does not show you what you return. You or other developers have to read the method to know whats the Name and whats the Surname.

Sample

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;
} 

Update

I suggest to use SingleOrDefault instead of Single. This will make sure you get a null result if the Account does not exist instead of throw a exception.

//
select new MyResult{ Name = p.FirstName, Surname = p.Surname }).SingleOrDefault();
//

Upvotes: 6

the_joric
the_joric

Reputation: 12261

Yet another (not the best :) ) option is to return an array:

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};
} 

Upvotes: 1

HatSoft
HatSoft

Reputation: 11201

How about returning it as Tuples as long as you dont mind using the retunred type this way tuple.Item1, tuple.Item2

Upvotes: 0

Phil.Wheeler
Phil.Wheeler

Reputation: 16858

If you're using .Net 4 you could return a dynamic instead of a string and grab both values directly from the returned object.

Upvotes: 0

Ade Stringer
Ade Stringer

Reputation: 2681

If you don't want to define a new object for your return type, you can use Tuple<string, string>.

Upvotes: 4

Related Questions