9

I'm in the process of converting my chart application from preset data to using a database.

Previously I was using this:

var data = new Dictionary<string, double>();            

switch (graphselected)
{        
case "1":
    data = new Dictionary<string, double>
    {
        {"Dave", 10.023f},
        {"James", 20.020f},
        {"Neil", 19.203f},
        {"Andrew", 4.039f},
        {"Steve", 5.343f}
    };
    break;
case "2":
    data = new Dictionary<string, double>
    {
        {"Dog", 10.023f},
        {"Cat", 20.020f},
        {"Owl", 19.203f},
        {"Rat", 16.039f},
        {"Bat", 27.343f}
    };
    break;
//etc...
}

// Add each item in data in a foreach loop
foreach (var item in list)
{
    // Adjust the Chart Series values used for X + Y
    seriesDetail.Points.AddXY(item.Key, item.Value);
} 

And this is what I'm trying to do:

var list = new List<KeyValuePair<string, double>>();

switch (graphselected)
{
case "1":
    var query = (from x in db2.cd_CleardownCore
                 where x.TimeTaken >= 10.0
                 select new { x.IMEI, x.TimeTaken }).ToList();
    list = query;                
    break;
//...
}

My code Error's on:

list = query; 

With Error :

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'
to 'System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,double>>'

How can I implement a conversion?

2
  • 5
    Does new { x.IMEI, x.TimeTaken } look like a KeyValuePair to you? Aug 28, 2014 at 16:33
  • Taken from another post: "LINQ needs a parameterless constructor because it wants to use collection initialization (the {} brackets). You can have additional class constructors, but LINQ will not use them." As i understand it this is the reason why its formatted as in my code. Source: stackoverflow.com/questions/15382840/…
    – Baggerz
    Aug 29, 2014 at 8:53

1 Answer 1

26

If you want a list of keyvaluepair, you need to build it with well, keyvaluepairs! replace your Anonymous object in the select as such :

select new KeyValuePair<string,double>(x.IMEI,x.TimeTaken)

Edited for your new issues:

var q = (from x in db2.cd_CleardownCore
             where x.TimeTaken >= 10.0
             select new { x.IMEI, x.TimeTaken });
var query = q.AsEnumerable() // anything past this is done outside of sql server
      .Select(item=>new KeyValuePair<string,double?>(item.IMEI,item.TimeTaken))
      .ToList();
5
  • 1
    Hello Ronan, thankyou for the assistance, i tried using the code above but due to my double being nullable i get: Argument 2: cannot convert from 'double?' to 'double' I tried changing this to select new KeyValuePair<string,double>(x.IMEI,(double)x.TimeTaken)).ToList(); but this now gives the following error: Only parameterless constructors and initializers are supported in LINQ to Entities.
    – Baggerz
    Aug 29, 2014 at 8:09
  • If your values can be nullable it makes no sense to cast them anyway, just use the code that i gave you and instead change the type of your "list" variable to be compatible as such var list = new List<KeyValuePair<string, double?>>(); Aug 29, 2014 at 8:25
  • thanks ronan, i tried the above but then it doesnt like double in select new KeyValuePair<string,double>(x.IMEI,x.TimeTaken) so i changed it to select new KeyValuePair<string,double?>(x.IMEI,x.TimeTaken) however when the code is run i get the same error: Only parameterless constructors and initializers are supported in LINQ to Entities.
    – Baggerz
    Aug 29, 2014 at 8:43
  • 1
    What it doesn't like is the new KeyValuePair (regardless of what you use) because it isn't a parameterless constructor, what you can do is bypass linq to entities for that and do this on the client side. Editing my post now. Aug 29, 2014 at 14:16
  • Thanks Ronan, the code works great and your explanations have been detailed so i understand now moving forward. :) Could I just ask you to update your edit again tho as i noticed the closing bracket was missing as below .Select(item=>new KeyValuePair<string,double?>(item.IMEI,item.TimeTaken))
    – Baggerz
    Aug 29, 2014 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.