访问 LINQ 结果集中的不同列

发布于 2024-10-19 11:10:54 字数 450 浏览 0 评论 0原文

我有一个使用 LINQ 从数据库返回多列的查询,

var donors = from d2 in db.Donors
                 where d2.Status == "Pending"
                 select new { donorID = d2.donorID, bloodGroup = d2.bloodGroup, contactNo = d2.contactMobile, status = d2.Status };

现在我想在访问 donors 结果集中的一列值的不同 Labels 中显示结果。

例如:

Label1.Text = 捐赠者ID; Label2.Text = BloodGroup; ...等等

请帮助我实现这一点。

I have a query which returns multiple columns from the database using LINQ

var donors = from d2 in db.Donors
                 where d2.Status == "Pending"
                 select new { donorID = d2.donorID, bloodGroup = d2.bloodGroup, contactNo = d2.contactMobile, status = d2.Status };

now I want to display the results in different Labels accessing one column value from donors resultset.

ex:

Label1.Text = donorID;
Label2.Text = bloodGroup;
...and so on

please help me achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在风中等你 2024-10-26 11:10:54

如果您要将标签设置为值,则需要一个单个记录。当前您正在选择一个记录序列。假设您只对第一个值感兴趣。您可以这样写:

var donors = from d2 in db.Donors
             where d2.Status == "Pending"
             select new { d2.donorID, d2.bloodGroup, 
                          contactNo = d2.contactMobile, status = d2.Status };

var firstDonor = donors.FirstOrDefault();
if (firstDonor != null)
{
    Label1.Text = firstDonor.donorID;
    Label2.Text = firstDonor.bloodGroup;
    // ...
}
else
{
    // There weren't any matching donors
}

如果您想显示所有捐赠者详细信息,您会需要更像网格的东西。

If you're going to set a label to a value, you'll need a single record. Currently you're selecting a sequence of records. Let's suppose you're only interested in the first value. You could write:

var donors = from d2 in db.Donors
             where d2.Status == "Pending"
             select new { d2.donorID, d2.bloodGroup, 
                          contactNo = d2.contactMobile, status = d2.Status };

var firstDonor = donors.FirstOrDefault();
if (firstDonor != null)
{
    Label1.Text = firstDonor.donorID;
    Label2.Text = firstDonor.bloodGroup;
    // ...
}
else
{
    // There weren't any matching donors
}

If you want to display all the donor details, you'll want something more like a grid.

风吹短裙飘 2024-10-26 11:10:54

如果你坚持保留标签,你可以这样做

var donors = from d2 in db.Donors
             where d2.Status == "Pending"
             select d2;

List<Donors> myDonors = donors.ToList();

        int x = 0;
        int y = 0;
        foreach (var donor in donors)
        {
            Label myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.donorID.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;

            myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.blodGroup.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;

            myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.contactNo.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;

            myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.status.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;


            y = 0;
            x += myLabel.Height + 5;
        }

If you insist in keeping the labels, you could do something like this

var donors = from d2 in db.Donors
             where d2.Status == "Pending"
             select d2;

List<Donors> myDonors = donors.ToList();

        int x = 0;
        int y = 0;
        foreach (var donor in donors)
        {
            Label myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.donorID.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;

            myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.blodGroup.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;

            myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.contactNo.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;

            myLabel = new Label();
            myLabel.Top = x;
            myLabel.Left = y;
            myLabel.Text = donor.status.ToString();
            panel1.Controls.Add(myLabel);
            y += myLabel.Width + 5;


            y = 0;
            x += myLabel.Height + 5;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文