C#Linq查询提取重复数据类型

时间:2019-03-07 09:46:23

标签: c# linq sorting

我有一个原始数据列表,其中一个时间戳记Date_Time总是有8个数据值可用。我想使用linq对每个Date_Time按Data_Type将Data_Value列的值排序为一行。

Rawdata table

我有以下课程,并希望将其作为该课程的列表返回。

public class MeasurementData
{
public MeasurementData();

public int Test_ID { get; set; }
public int Channel { get; set; }
public string Date_Time { get; set; }
public double Current { get; set; }
public double Voltage { get; set; }
public double Charge_Capacity { get; set; }
public double Discharge_Capacity { get; set; }
}

这是代码的简化形式,我只是尝试提取四个值。

public static List<DataStructure.MeasurementData>     RawResult(List<DataStructure.MeasurementRawTableSQL> rawData, int _Test_ID)
{
if (rawData != null)
{
    var result = rawData.GroupBy(x => x.Date_Time)
         .Select(gr =>
         {
             var _Date_Time = TicksToDate(gr.FirstOrDefault().Date_Time);
             var _Channel = gr.FirstOrDefault().Channel;
             var _Voltage = gr.Where(x => x.Data_Type == 21).FirstOrDefault().Data_Value;
             var _Current = gr.Where(x => x.Data_Type == 22).FirstOrDefault().Data_Value;
             var _Charge_Capacity = gr.Where(x => x.Data_Type == 23).FirstOrDefault().Data_Value;
             var _Discharge_Capacity = gr.Where(x => x.Data_Type == 24).FirstOrDefault().Data_Value;

             return new DataStructure.MeasurementData
             {
                 Test_ID = _Test_ID,
                 Channel = _Channel,
                 Date_Time = _Date_Time,
                 Current = _Current,
                 Voltage = _Voltage,
                 Charge_Capacity = _Charge_Capacity,
                 Discharge_Capacity = _Discharge_Capacity
             };
         }
         ).ToList();

    return result;
}
else return null;
}

这在某种程度上有效,对于案例21和22,它给了我适当的值,而对于案例23和24,我得到一个错误“对象引用未设置为对象的实例”。另一方面,数据库每个数据点都有此行,并且永远不会为null。如果我只选择First()而不是FirstOrDefault(),则会得到“序列不包含任何元素”的信息。

我现在真的很困,真的会为您提供帮助。

1 个答案:

答案 0 :(得分:0)

以下是解决问题的最干净的方法。我无法解释为什么您没有为每个时间值获取Data_Type,但是下面的代码应该可以避免出现异常。我用十进制?作为类型。当WHERE返回null时,将导致异常,您将无法获得属性Data_Value。因此,您必须测试以下类似null的代码:

    class Program
    {
        static void Main(string[] args)
        {
        }
        //public DataStructures DataStructure = new DataStructures();

        public static List<DataStructure.MeasurementData> RawResult(List<DataStructure.MeasurementRawTableSQL> rawData, int _Test_ID)
        {
            if (rawData != null)
            {
                double? _Voltage = null;
                double? _Current = null;
                double? _Charge_Capacity = null;
                double? _Discharge_Capacity = null;

                var result = rawData.GroupBy(x => x.Date_Time)
                     .Select(gr =>
                     {
                         var _Date_Time = gr.Key;
                         var _Channel = gr.FirstOrDefault().Channel;
                         var _Voltage_Row = gr.Where(x => x.Data_Type == 21).FirstOrDefault();
                         if(_Voltage_Row != null) _Voltage = _Voltage_Row.Data_Value;
                         var _Current_Row = gr.Where(x => x.Data_Type == 22).FirstOrDefault();
                         if(_Current_Row != null) _Current = _Current_Row.Data_Value;
                         var _Charge_Capacity_Row = gr.Where(x => x.Data_Type == 23).FirstOrDefault();
                         if (_Charge_Capacity_Row != null) _Charge_Capacity = _Charge_Capacity_Row.Data_Value; 
                         var _Discharge_Capacity_Row = gr.Where(x => x.Data_Type == 24).FirstOrDefault();
                         if (_Discharge_Capacity_Row != null) _Discharge_Capacity = _Discharge_Capacity_Row.Data_Value; 

                         return new DataStructure.MeasurementData
                         {
                             Test_ID = _Test_ID,
                             Channel = _Channel,
                             Date_Time = _Date_Time,
                             Current = _Current,
                             Voltage = _Voltage,
                             Charge_Capacity = _Charge_Capacity,
                             Discharge_Capacity = _Discharge_Capacity
                         };
                     }
                     ).ToList();

                return result;
            }
            else return null;
        }
    }
    public class DataStructure
    {

        public class MeasurementData
        {
            public int? Test_ID { get; set; }
            public int? Channel { get; set; }
            public DateTime Date_Time { get; set; }
            public double? Current { get; set; }
            public double? Voltage { get; set; }
            public double? Charge_Capacity { get; set; }
            public double? Discharge_Capacity { get; set; }
        }

        public class MeasurementRawTableSQL
        {
            public DateTime Date_Time { get; set; }
            public int Channel { get; set; }
            public int Data_Type { get; set; }
            public double Data_Value { get; set; }

        }
    }