在标签中显示linq查询结果

时间:2013-11-13 09:51:04

标签: c# linq datacontext

我试图在文本框中显示结果linq查询显示。我在SensorDataPackages表中有OID,readTime,DeviceID,a_TankLevel1,a_TankLevel2。我想在查询返回后在文本框中显示某些字段的值

Textbox1 = a_TankLevel1;
Textbox2 = a_TankLevel2;
等等。

我得到“SensorData不为空”。查询正在运行。

//我创建了新的modeldataset

private string istDeviceID = "";

  MyWebApplication.MyClasses.ModelDataContext MyNewModel= new MyWebApplication.MyClasses.ModelDataContext();  

        this.istDeviceID = ComboBox1.SelectedItem.GetValue("DeviceID").ToString();

        DateTime SensorDateNow= DateTime.Now;
        DateTime SensorDate= DateTime.Now.AddDays(-7);

        var SensorData = (from Sensor in MyNewModel.SensorDataPackages
        where Sensor.DeviceID == int.Parse(istDeviceID)
        where Sensor.readTime.Date >= SensorDateNow.Date
        where Sensor.readTime.Date <= SensorDate.Date
        select Sensor).ToList();                

       if (SensorData.Count > 0)          
       {
           MessageBox.Show("SensorData is not empty");              
           textbox1.text = SensorData.?????? // I need help here !!!
       }
       else
       {
           MessageBox.Show("SensorData is empty");
       }

1 个答案:

答案 0 :(得分:3)

尝试String.Join

textbox1.text= String.Join(",", SensorData.Select(a => a.ToString()));

这假设您的SensorData对象具有被覆盖的ToString()

编辑:

获取最后一个值尝试此

textbox1.text= SensorData.Last().a_TankLevel1.ToString();