-2

net core project. I am trying to make below object from my c# code.

 {
    "temperature_Mean_1_0":
      {"Header":"Air Temp","Type":"temperature","Height":"2","SubType":"Min","BoomOrientation":"0.0", 
      "NorthReference":"true_north","Unit":"C","Scale": 
      "2.0","Offset":"0.0","DVLpyName":"temperature_1_1","FirstData":"26.98","Converted":"26.98", 
     "AssociatedDirection":"1"
      }
 }

Below is my code

IEnumerable<MastSetupInformation> mastSetupInformations = await _mastSetupInformationRepository.GetAsync(x => Ids.Contains(x.Id));
 var data = (from l in mastSetupInformations
                        select (new Dictionary<string, object> { { l.Channel,
                                 new MastSetupInformationAdfList {
                                     MastSetupId = l.Id ,
                                     Header = l.Header,
                                     FirstData = l.FirstData,
                                     Type = l.Type,
                                     SubType = l.SubType,
                                     Unit = l.Unit,
                                     Height = l.Height,
                                     BoomOrientation = l.BoomOrientation,
                                     Scale = l.Scale,
                                     Offset = l.Offset,
                                     Converted = l.Converted,
                                     DvlpyName = l.DvlpyName,
                                     AssociatedDirection = l.AssociatedDirection,
                                     NorthReference = MastLocation.NorthReference
                                     } } }));

But in my code data variable holds below value

[
            {
            "temperature_Mean_1_0": {
                    "MastSetupId": 903,
                    "Header": "Horizontal Wind Speed Std. Dev.  at 99m [m/s]",
                    "FirstData": "1.45",
                    "Type": "WindDirection",
                    "SubType": "STDev",
                    "Unit": "m/s",
                    "Height": 50,
                    "BoomOrientation": 0,
                    "Scale": 3,
                    "Offset": 0
                }
            }
]

I tried something below

object dataArray = data.Cast<object>().ToArray();

This dint work out so I am struggling to find what I am missing. Can someone help me to understand the issue. Any help would be appreciated. Thank you

Mr Perfect
  • 585
  • 8
  • 30
  • You have a collection of dictionary's in `data`. what is it you actually want form that? A flattened list of some object ? – TheGeneral Sep 10 '20 at 09:11
  • one more example, where writing `var data` is masking what you have done wrong. Please try to write the type explicitly instead of using `var`. This will give you much more detailed information about your problem. And it will give us an idea what you actually are trying to achieve. – Mong Zhu Sep 10 '20 at 09:12
  • maybe you want `data.SelectMany(x => x.Values).ToArray()` – TheGeneral Sep 10 '20 at 09:14
  • Are you simply stating that your code is returning a list of items, and you want a single object? The object you state you want looks very much like the *only object in your list of objects* in what you say you are ending up with. If all you want is a single object, rather than an Enumerable one, then you could just use `var result = data.FirstOrDefault()`? Oh, and as others have pointed out your examples and stuff are all JSON... but I'm assuming (since you're showing JSON) that you are happy you can get JSON from your .Net objects... – GPW Sep 10 '20 at 09:17
  • `object dataArray = data.Cast().ToArray();` - Try `var dataArray = data.Cast().ToArray();` - Not sure about the Cast, though.. – TaW Sep 10 '20 at 12:36

1 Answers1

1

Looks like a JSON object. Try using JSON.Convert() and JSON.Deserialize() methods. Go through this - Deserialize JSON with C#

Gaurav Gupta
  • 129
  • 1
  • 13