【发布时间】:2012-07-24 22:20:13
【问题描述】:
您好,我创建了一个类来从客户的帐户表中获取值,我为此创建了一个类。我想通知 AccountId 和我想要返回结果的表的字段(例如 FirstName)。实现这一目标的最佳方法是什么?我得到了类似下面的东西作为我的替代方案,但我无法让它工作......
这就是我想要得到名字结果的方式:
LabelFirstName.Text = Manage.GetAccount(2, "FirstName"); // where 2 is the id I informed and FirstName is the Column I want to retrieve from the table.
例如,结果将是“John”。
这就是我所拥有的:
public class Manage
{
public Manage()
{
}
public static string GetAccount(int AccountId, string Field)
{
LinqSqlDataContext contextLoad = new LinqSqlDataContext();
var q = (from p in contextLoad.MyAccounts
where p.AccountId == AccountId
select p).Single();
string var = q.?? // ?? would be my Field string "FirstName" for example
return var;
}
}
请帮忙
谢谢
【问题讨论】:
-
虽然肯定有一些很棒的答案(Reflection 一个)关于如何实现你想要的我想先问这个:你想要实现什么在这里,因为恐怕我在这里看到了一个很大的反模式,或者至少是一些重新发明轮子。如果您需要名字、地址街道、电话号码和电子邮件,您会怎么做?调用此函数 4 次?
-
是的,Roblll,我想在网页中填写一份表格,其中包含此帐户、姓名、姓氏、街道等的所有详细信息……所以我想我必须打电话给功能太多次......在这种情况下你会推荐什么?在页面本身中运行 linq 查询是更好的解决方案吗?谢谢。
-
@Gurizao:你的问题解决了吗??
-
@Gurizao 然后返回 q,例如整个“帐户对象”(如demonstrated by SidAhmed),以便您可以在视图中访问它的属性。
标签: c# asp.net string linq return