c# - Linq 查询不返回值

标签 c# linq

<分区>

  • 框架:.Net 4.5

我正在使用我的数据库中的数据填充文本框字段。但是,调试后我发现我的 Linq 查询没有返回值。

protected void btnChoose_Click(object sender, EventArgs e)
{
    API_DatabaseEntities1 db = new API_DatabaseEntities1();

    if (ddlCustomer.SelectedValue == "Marisol") {

        tbDescription.Text = (from c in db.Customers
                                where c.CustomerID == 4
                                select c.ProductDescription).ToString();
        tbFName.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.Fname).ToString();
        tbSocial.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.SSN).ToString();
        tbDOB.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.DOB).ToString();
        tbFName1.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.Fname).ToString();
        tbMName.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.Mname).ToString();
        tbLName.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.Lname).ToString();
        tbPrimaryPhone.Text = (from c in db.Customers
                                where c.CustomerID == 4
                                select c.PrimaryPhone).ToString();
        tbSecondaryPhone.Text = (from c in db.Customers
                                    where c.CustomerID == 4
                                    select c.SecondaryPhone).ToString();
        tbAdd1.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.Address).ToString();
        tbCity.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.City).ToString();
        tbZip.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.Zip).ToString();
        tbEmail.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.Email).ToString();
        tbMonLease.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.MortLeaseAmt).ToString();
        tbEmployer.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.Employer).ToString();
        tbPosition.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.Position).ToString();
        tbHireDate.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.Position).ToString();
        tbWorkPhone.Text = (from c in db.Customers
                            where c.CustomerID == 4
                            select c.WorkPhone).ToString();
        tbGross.Text = (from c in db.Customers
                        where c.CustomerID == 4
                        select c.GrossIncome).ToString();

    }

    Debug.WriteLine(tbGross.Text);
}

此时我不确定我的数据库连接或我的 Linq 查询是否存在问题。感谢您提供的任何帮助。

最佳答案

我认为您的意思是获得一个客户而不是列表。

select 将返回一个集合而不是一个项目,像这样更改它:

  var customer = (from c in db.Customers
                 where c.CustomerID == 4
                 select c).FirstOrDefault();

注意 FirstOrDefault() 的调用,这将返回 1 个客户而不是包含 1 个客户的列表。

FirstOrDefault() 如果查询未返回任何内容,将返回 null:

 if(customer == null)
     return; // no customer found

然后您可以像这样填充您的文本字段:

   tbDescription.Text = customer.ProductDescription;
   tbFName.Text = customer.Fname;

   etc.

关于c# - Linq 查询不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34815863/

相关文章:

c# - 如何在 lambda 表达式或 linq 中编写以下代码?

html - 我如何解决 System.Collections.Generic.List 没有 ProductID 的定义或 ProductID 的方法?

c# - 从特定的选项卡索引开始并在末尾停止

c# - 是否可以将 Excel/CSV 数据从剪贴板粘贴到 C# 中的 DataGridView?

即使字符串值按预期匹配,C# 字符串比较的计算结果也不为 true

C# 如何替换系统托盘时钟

c# - DataAnnotations 属性是否已缓存?如果是这样,如何在不同文化之间切换?

c# - 从列表中按 ID 选择

c# - 附加的 linq 查询的等效 T-SQL 选择查询是什么?

c# - 什么时候应该关心在开始下一个方法之前完成一种方法?