Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq查询未返回值 框架:.Net 4.5_C#_Linq - Fatal编程技术网

C# Linq查询未返回值 框架:.Net 4.5

C# Linq查询未返回值 框架:.Net 4.5,c#,linq,C#,Linq,我正在用数据库中的数据填充文本框字段。但是,在调试之后,我发现我的Linq查询没有返回值 protected void btnChoose_Click(object sender, EventArgs e) { API_DatabaseEntities1 db = new API_DatabaseEntities1(); if (ddlCustomer.SelectedValue == "Marisol") { tbDescription.Text = (fr

我正在用数据库中的数据填充文本框字段。但是,在调试之后,我发现我的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);
}

此时,我不确定我的DB连接或Linq查询是否存在问题。我很感激你能提供的任何帮助

我想你的意思是要得到一个客户而不是一张名单

选择
将返回一个集合而不是一个项目,请按如下方式更改:

  var customer = (from c in db.Customers
                 where c.CustomerID == 4
                 select c).FirstOrDefault();
   tbDescription.Text = customer.ProductDescription;
   tbFName.Text = customer.Fname;

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

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

 if(customer == null)
     return; // no customer found
然后,您可以按如下方式填充文本字段:

  var customer = (from c in db.Customers
                 where c.CustomerID == 4
                 select c).FirstOrDefault();
   tbDescription.Text = customer.ProductDescription;
   tbFName.Text = customer.Fname;

   etc.
请这样做:

var oneRec = (from c in db.Customer
                  where c.CustomerID == 4
                  select c).FirstOrDefault();

tbDescription.Text = oneRec.ProductDescription;
tbFName.Text = oneRec.Fname;
tbSocial.Text = oneRec.SSN;

// ect

“不返回值”哪一个?通过一个查询获取所有客户属性不是更好吗?与其每次都使用新的查询来获取属性?@Ian此时,没有一个查询返回值。仅供参考:您使用一系列单独的查询来冲击数据库,以从同一记录中提取单个值。您应该进行一次查询以获取所有值。@这可能是您的查询首先失败的原因吗?在尝试读取数据之前,您还应该检查以确保customer不为nullvalues@snow_FFFFFF事实上,这是固定的thanks@thumbmunkeys非常感谢。现在,customer对象似乎没有返回null。但是,当我将DB值等同于文本框ID时,这些值仍然为空。当通过调试运行“customer”时,它返回一个冗长的数值,而不是查询结果。想法?嗯,没有更多信息很难说,但你肯定有一位客户的
CustomerId==4
?@thumbmunkeys不是问题。我当然明白。我做了双重检查,只是为了确保,是的,有一个CustomerID==4。我的每个文本框变量都与正确的TB ID直接相关,因此我不能完全确定为什么这些字段返回空白。也许,我会尝试使用不同的CustomerID,看看这是否会有所不同。