Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Generic List<anonymous type > to Generic List <Classname>?

Tags:

c#

linq

I have a database, where the CPU usage is inserted for every 'n' seconds and my task is to get the latest CPU entry from the database. I would like to display the latest CPU entry from the database tat belongs to the particular server , and I would like to save the result into a generic list.

what I have tried,

    string match = "ServerX"

    List<UsageCPU> cpus = (from a in db.UsageCPUs
                          where a.ServerID.Contains(match)
                          group a by a.ServerID into b
                          orderby b.Key
                          select new { ServerID = b.Key, 
                          Usage = b.FirstOrDefault() }).ToList();

and I would like to loop through the result cpus to perform further other steps.

but I get the following error

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FNC_RAQIT_HM_UI.UsageCPU>'.

EDIT

enter image description hereUPDATE The following query from Satpal worked for me.. but using var

   var cpus = (from a in db.UsageCPUs
                    where a.ServerID.Contains(match)
                    group a by a.ServerID into b
                    orderby b.Key
                    select new
                    {
                        ServerID = b.Key,
                        Usage = b.FirstOrDefault().Usage
                    }).ToList();

        foreach (var item in cpus)
        {
           if(bla bla..)
        }

kindly help !!

like image 831
demo_user Avatar asked Jun 24 '13 12:06

demo_user


2 Answers

Use

List<UsageCPU> cpus = (from a in db.UsageCPUs
                      where a.ServerID.Contains(match)
                      group a by a.ServerID into b
                      orderby b.Key
                      select new UsageCPU  //Added UsageCPU  
                      { 
                          ServerID = b.Key, 
                          Usage = b.FirstOrDefault().Usage 
                      }).ToList();

Update as per error

The entity cannot be constructed in a LINQ to Entities query

List<UsageCPU> cpus = (from a in db.UsageCPUs
                      where a.ServerID.Contains(match)
                      group a by a.ServerID into b
                      orderby b.Key
                      select new 
                      { 
                          ServerID = b.Key, 
                          Usage = b.FirstOrDefault().Usage
                      })
                    .AsEnumerable()
                    .Select(x => new UsageCPU  
                    {
                       ServerID ,
                       Usage            
                    }).ToList();
like image 56
Satpal Avatar answered Sep 19 '22 12:09

Satpal


try to select a not new when you select new you get anonymous type which can't be casted to UsageCPU type

 (from a in db.UsageCPUs
 where a.ServerID.Contains(match)
 group a by a.ServerID into b
 orderby b.Key
 select a)

or use var and then loop through the results

var cpus = (from a in db.UsageCPUs
                      where a.ServerID.Contains(match)
                      group a by a.ServerID into b
                      orderby b.Key
                      select new { ServerID = b.Key, 
                      Usage = b.FirstOrDefault() }).ToList();
like image 26
Guru Stron Avatar answered Sep 21 '22 12:09

Guru Stron