无法转换类型&System; .Linq.IGrouping`2 [[System.Decimal

时间:2017-08-02 11:58:04

标签: c# entity-framework linq azure azure-sql-database

在我的webApi2服务中,我使用Entity框架作为我的Linm作为查询语言后端作为azure-sql。 我的DTO类来映射我的查询结果。

public class RTLSLevelZonesInfoDTO
{
    public List<int> L1 { get; set; }
    public List<int> L2 { get; set; }
    public List<int> L3 { get; set; }

    public RTLSLevelZonesInfoDTO()
    {

        L1 = new List<int>();

        L2 = new List<int>();
        L3 = new List<int>();

    }
}

我填写数据的查询。

var leveZones =  (from s in db.RTLS_SpotLevelMap
                        group s by s.Level_no
                        into g
                        select new RTLSLevelZonesInfoDTO
                        {
                            L1 = (g.Key == 1) ? (List<int>)g: convertedLocationArray,
                            L2 = (g.Key == 2) ? (List<int>)g : convertedLocationArray,
                            L3 = (g.Key == 3) ? (List<int>)g : convertedLocationArray

                        }).FirstOrDefault();

上面的查询提取属于特定级别的zoneids(我的数据库中只有3个固定级别,如下图所示)

enter image description here

执行我的代码后,我发现了一条带有消息

的异常
  

无法转换类型&#39; System.Linq.IGrouping2 [[System.Decimal,   mscorlib,版本= 4.0.0.0,文化=中性,   公钥= b77a5c561934e089],[RTLSWebService.Models.RTLS_DB.RTLS_SpotLevelMap,   RTLSWebService,版本= 1.0.0.0,文化=中立,   公钥=空]]&#39;输入   &#39; System.Collections.Generic.List`1 [[System.Int32,mscorlib,   Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]&#39;。   LINQ to Entities仅支持转换EDM原语或枚举   类型。

3 个答案:

答案 0 :(得分:1)

试试这样:

Def file_Q (fileaddr):
 Train_labels = []
 Train_images = []

 For line in open (fileaddr, 'r'):
  Cols = re.split (',', line)
  Train_images.append (cols [0])
  Train_labels.append (int (cols [1]))

 Input_queue = tf.train.slice_input_producer ([train_images, train_labels], num_epochs = None, shuffle = True)

 Return input_queue # return [addr text, label] queue


Def read_data (input_queue):

 Image_addr = input_queue [0]
 Label = input_queue [1]
 Image = tf.image.decode_jpeg (tf.read_file (image_addr), channels = 3) # create 3-d rgb image object using image addr
 Print 'image_addr:', image_addr
 Print 'label:', label
 Print 'image:', image
 Return image, label, image_addr

Def read_data_batch (FILE_ADDR_):
 Input_queue = file_Q (FILE_ADDR_)
 Image, label, filename = read_data (input_queue)
 Image = tf.reshape (image, [1344, 200, 3])
 Batch_image, batch_label = tf.train.batch ([image, label], batch_size = 32) #, capacity = 20000, min_after_dequeue = 100)
 Return batch_image, batch_label

Bat_im, bat_la = read_data_batch (FILE_ADDR)

With tf.Session () as sess:
 Sess.run (tf.local_variables_initializer ())
 Sess.run (tf.global_variables_initializer ())
 Coord = tf.train.Coordinator ()
 Threads = tf.train.start_queue_runners (coord = coord, sess = sess)

 For i in range (10):
  Batch_im_, batch_la_ = sess.run ([bat_im, bat_la])
  Print batch_im_, batch_la_

 Coord.request_stop ()
 Coord.join (threads)
 Print ('finish')

答案 1 :(得分:0)

最后,我通过对每个列表进行单独查询来获得解决方案。

  var levelZones = new RTLSLevelZonesInfoDTO();

  var l1 = await ((db.RTLS_SpotLevelMap.Where(p => p.Level_no == 1)).Select(q => (int)q.zone_id)).ToListAsync();
  var l2 = await ((db.RTLS_SpotLevelMap.Where(p => p.Level_no == 2)).Select(q => (int)q.zone_id)).ToListAsync();
                        var l3 = await ((db.RTLS_SpotLevelMap.Where(p => p.Level_no == 3)).Select(q => (int)q.zone_id)).ToListAsync();

  levelZones.L1 = l1;
  levelZones.L2 = l2;
  levelZones.L3 = l3;

答案 2 :(得分:0)

这个怎么样?:

var lookup = db.RTLS_SpotLevelMap.ToLookup(s => s.Level_no, s => s.ZoneId);
new RTLSLevelZonesInfoDTO
{
    L1 = lookup[1].ToList(),
    L2 = lookup[2].ToList(),
    L3 = lookup[3].ToList()
};