Linq查询不返回值

时间:2016-01-15 16:44:03

标签: c# linq

  • 框架:.Net 4.5

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

KERNEL[767249.530132] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4 (usb)
ACTION=add
BUSNUM=001
DEVNAME=bus/usb/001/020
DEVNUM=020
DEVPATH=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4
DEVTYPE=usb_device
MAJOR=189
MINOR=19
PRODUCT=403/ffa8/600
SEQNUM=2777
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3

KERNEL[767249.531432] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0
DEVTYPE=usb_interface
INTERFACE=255/255/255
MODALIAS=usb:v0403pFFA8d0600dc00dsc00dp00icFFiscFFipFF
PRODUCT=403/ffa8/600
SEQNUM=2778
SUBSYSTEM=usb
TYPE=0/0/0
UDEV_LOG=3

KERNEL[767249.531499] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0/ttyUSB0 (usb-serial)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0/ttyUSB0
SEQNUM=2779
SUBSYSTEM=usb-serial
UDEV_LOG=3

KERNEL[767249.531767] add      /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0/ttyUSB0/tty/ttyUSB0 (tty)
ACTION=add
DEVNAME=ttyUSB0
DEVPATH=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.4/1-1.4:1.0/ttyUSB0/tty/ttyUSB0
MAJOR=188
MINOR=0
SEQNUM=2780
SUBSYSTEM=tty
UDEV_LOG=3
----------------------------------------------
KERNEL[767266.871139] add      /devices/virtual/net/slcan0 (net)
ACTION=add
DEVPATH=/devices/virtual/net/slcan0
IFINDEX=32
INTERFACE=slcan0
SEQNUM=2781
SUBSYSTEM=net
UDEV_LOG=3

KERNEL[767266.871189] add      /devices/virtual/net/slcan0/queues/rx-0 (queues)
ACTION=add
DEVPATH=/devices/virtual/net/slcan0/queues/rx-0
SEQNUM=2782
SUBSYSTEM=queues
UDEV_LOG=3

KERNEL[767266.871214] add      /devices/virtual/net/slcan0/queues/tx-0 (queues)
ACTION=add
DEVPATH=/devices/virtual/net/slcan0/queues/tx-0
SEQNUM=2783
SUBSYSTEM=queues
UDEV_LOG=3

KERNEL[767266.872198] move     /devices/virtual/net/can-aapje (net)
ACTION=move
DEVPATH=/devices/virtual/net/can-aapje
DEVPATH_OLD=/devices/virtual/net/slcan0
IFINDEX=32
INTERFACE=can-aapje
SEQNUM=2784
SUBSYSTEM=net
UDEV_LOG=3

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

2 个答案:

答案 0 :(得分:3)

我认为你的意思是得到一个顾客,而不是一个清单。

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.

答案 1 :(得分:0)

请这样做:

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