2012-07-26 136 views
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 ??; 
} 
+0

假设你想要的返回类型也是正确的,你不只是想返回string.Format(“{0} {1}”,q.Name,q.Surname);' – 2012-07-26 11:50:36

回答

4

如果您不想为返回类型定义新对象,则可以使用Tuple<string, string>

5

您可以返回强类型类,动态对象或元组。 我更喜欢返回一个强类型的类。

使用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(); 
// 
+0

这段代码假设一个从查询中只返回一个项目。如果集合中只有一个元素,Single将引发异常。 – 2012-07-26 11:47:31

+0

是的,因为AccountId应该是唯一的。 – dknaack 2012-07-26 11:48:29

+0

是的,有什么问题? – dknaack 2012-07-26 12:29:14

0

如果您使用的是.Net 4,您可以返回动态而不是字符串,并直接从返回的对象中获取两个值。

+1

只在同一个程序集内,或者如果您使用InternalsVisibleToAttribute。匿名类型是隐式的“内部”,这会导致“动态”问题。 – 2012-07-26 11:46:30

+1

这将会涉及DLR的开销,因为可以使用新类或Tuple 来实现。另外你会失去智能感知。 – 2012-07-26 11:46:32

0

如何通过引用,只要你使用retunred型这样tuple.Item1不介意返回它作为Tuples,tuple.Item2

1

通行证中有两个对象,你可以直接设置。

修改,可以一试功能的更少的代码气味版本

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 

}

+0

out params are not good – 2012-07-26 11:52:47

+0

@the_joric无法解释的清晰陈述的一点点,你现在已经让我所有人都担心了。 – 2012-07-26 11:53:47

+0

out参数的定义并不好,很多编码者认为它们会导致代码异味,因为它们会导致副作用。但是他们确实有他们的用途 - 尤其是在Try功能的样式中。 [见SO问题](http://stackoverflow.com/questions/281036/are-out-parameters-a-bad-thing-in-net) – 2012-07-26 11:59:32

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

比你能得到你的姓作为关键的姓。

+0

但是,你可以轻松地返回一个'new DictionaryEntry (q.FirstName,q.Surname)',它具有更强的类型优势,但Tuple 更好。 – 2012-07-26 14:05:31