C# 在静态类中返回多个值
如何在下面的类中返回名字和姓氏C# 在静态类中返回多个值,c#,asp.net,string,linq,return,C#,Asp.net,String,Linq,Return,如何在下面的类中返回名字和姓氏 public static string GetAccount(int AccountId) { LinqSqlDataContext contextLoad = new LinqSqlDataContext(); var q = (from p in contextLoad.MyAccounts where p.AccountId == AccountId select new
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 ??;
}
如果不想为返回类型定义新对象,可以使用
Tuple
返回强类型类、动态对象或Tuple。
我更喜欢返回强类型类
使用dynamic
类型的问题在于,您无法获得
intellisense和异常仅在运行时出现
元组的问题在于它没有显示什么
你回来了。您或其他开发人员必须阅读该方法
知道你的名字和姓氏
样品
更新
我建议使用SingleOrDefault
而不是Single
。这将确保你
如果帐户不存在,则获取null
结果,而不是引发异常
//
select new MyResult{ Name = p.FirstName, Surname = p.Surname }).SingleOrDefault();
//
如果您使用的是.Net 4,您可以返回一个动态而不是字符串,并直接从返回的对象中获取这两个值。如果您不介意以这种方式使用retunred类型,则返回该值如何tuple.Item1,tuple.Item2通过引用传入两个对象,您可以设置它们 经过修改,使其成为一个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
}另一个(不是最好的:)选项是返回数组:
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};
}
您可以使用哈希表来避免创建新的结果类。 大概是这样的:
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);
}
然后,您可以通过名作为键来获取姓氏。仅在同一程序集中,或者如果您使用InternalsVisibleToAttribute。匿名类型隐式为“内部”,这会导致
动态
出现问题。这将导致DLR参与新类或元组
实现的开销。此外,您还将丢失IntelliSense。此代码假定查询返回一个且仅返回一个项<如果集合中没有一个元素,则code>Single会引发异常。这很有效!我的目标是从account表中获取数据,并在网页的文本框中填入详细信息:姓名、地址等。。我得到的数据如下:FirstNameTextBox.Text=MyResults.GetAccount(AccountId).FirstName;姓氏TextBox.Text=MyResults.GetAccount(AccountId);AddressTextBox.Text=MyResults.GetAccount(AccountId).Address。等等这是最有效的上课方式吗?似乎每次我填写一个文本框时,我都必须调用一个类。。。有更好的选择吗?非常感谢,假设您希望返回类型也正确,并且您不只是想要返回string.Format(“{0}{1}”,q.Name,q.姓氏)代码>一个无关紧要的语句的TythJoJIT位,你让我现在都很担心。参数不被定义为坏,许多编码器认为它们是代码气味,因为它们会产生副作用。然而,它们确实有自己的用途——特别是在Try风格的函数中@好奇的是,它们怎么会产生副作用呢?编译器在赋值之前停止使用out
参数,调用者必须显式地指定out
,以便他们知道他们提供的任何变量都将被更改。副作用往往是不必要的状态更改。@Adamhuldsworth它们被视为副作用,因为每个变量实际上都作为对同一内存位置的引用传递到函数中,如果函数启动异步进程,则在函数返回后,该值可能会被更改。@BobVale谢谢,我必须记住这一点。我看到它们在我正在处理的当前代码库中四处游荡。但是,你也可以很容易地返回一个新的DictionaryEntry(q.FirstName,q.姓氏)
,它具有更强的键入功能,但元组更好。
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);
}