(List<Dictionary<Object, Object>> in Linq to extract data - c#

I have a data definition
I Deserialize JSON to this object
#return is JSON
JsonConvert.DeserializeObject<List<Dictionary<Object, Object>>>(utils.RemoveJsonOuterClass("GetTable", JsonConvert.DeserializeObject(#return).ToString()));
olist = [
[{
"item": 1
"Name "One"
}],
[{
"item": 2
"Name "Two"
}],
[{
"item": 1
"Name "One Two"
}]
];
This is a List<Dictionary<Object, Object>>
I need to find all of the items where "item" == 1.
Can I Use Linq? or is there any other way while using a large amount of data?

First: Your json is not correct fix that.
A colon should be present between Name and value.
A comma should be present after item value
and then change your code as below
//Create a class matching response object
public class ResponseItem
{
[JsonProperty("item")]
public int Item { get; set; }
public string Name { get; set; }
}
var responseJson = utils.RemoveJsonOuterClass("GetTable",
JsonConvert.DeserializeObject(#return).ToString();
var responseData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<ResponseItem, ResponseItem>>>(responseJson);
Then use foreach with Where and apply condition
foreach (var responseObject in responseData.Where(x=>x.First().Item.Equals(1)))
{
}
Where is deferred execution and on each loop, it returns an object.
Here is the screenshot of my local execution.

Don't know if u're right with the object type. But the task is easy to solve:
static void Main(string[] args)
{
// Build the object
List<Dictionary<int, TestObject>> list = new List<Dictionary<int, TestObject>>();
// fill it with dictionaries
list.Add(new List<TestObject>()
{
new TestObject(){ Id = 1, Name = "One" },
new TestObject() { Id = 2, Name = "Two" },
new TestObject() { Id = 3, Name = "Three" }
}.ToDictionary(d => d.Id));
list.Add(new List<TestObject>()
{
new TestObject() { Id = 2, Name = "Two" },
new TestObject() { Id = 3, Name = "Three" }
}.ToDictionary(d => d.Id));
list.Add(new List<TestObject>()
{
new TestObject(){ Id = 1, Name = "One" },
new TestObject() { Id = 2, Name = "Two" }
}.ToDictionary(d => d.Id));
// Let's build a single list to work with
IEnumerable<TestObject> completeList = list.SelectMany(s => s.Values);
// aaaand filter it
IEnumerable<TestObject> filteredList = completeList.Where(l => l.Id == 1);
}
public class TestObject
{
public int Id { get; set; }
public string Name { get; set; }
}
Most part is initialization ;-)

Related

Convert Flat Data from C# to JSON

I have a c# class that looks something like this:
public class Item
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
}
and i would like to convert it to json where json property name is item name and its value is item description. if some item has any children then i would like the json property name to stay as item name and the children to be added like item name:item description(the parent item description and type become empty string if it has any children, except when the type is array). The type has following values: array, string, int, object. if the item type is an array then each time a child is added to the type array item, the child is item description. so i would like those values to be added to the json array as well.
if the type is string or int the json property value should be int or string.
I am trying to write custom JsonSerializer but i am getting nowhere.
so if i have a list with items:
List<Item> MyItems = new List<Item>()
{
new Item { Id = 1, ParentId = null, Name = "Name1", Description = "", Type = "" },
new Item { Id = 2, ParentId = 1, Name = "Name2", Description = "Description1", Type = "String" },
new Item { Id = 3, ParentId = 1, Name = "Name3", Description = "", Type = "Array" },
new Item { Id = 4, ParentId = 3, Name = "", Description = "ArrayItem1", Type = "" },
new Item { Id = 5, ParentId = 3, Name = "", Description = "ArrayItem2", Type = "" },
new Item { Id = 6, ParentId = 3, Name = "", Description = "ArrayItem3", Type = "" },
new Item { Id = 7, ParentId = null, Name = "Name4", Description = "5", Type = "Int" },
};
then the json should like this:
{
"name1":{
"name2":"description1",
"name3":[
"ArrayItem1",
"ArrayItem2",
"ArrayItem1"
]
},
"name4":5
}
Here's an extension method that I think does what you want:
public static class Ex
{
public static string ToJson(this List<Item> items)
{
var lookup = items.ToLookup(x => x.ParentId);
JObject ToJson(int? parentId)
{
JProperty ToProperty(Item item)
{
switch (item.Type)
{
case "":
return new JProperty(item.Name, ToJson(item.Id));
case "String":
return new JProperty(item.Name, item.Description);
case "Array":
return new JProperty(item.Name, lookup[item.Id].Select(x => x.Description).ToArray());
case "Int":
return new JProperty(item.Name, int.Parse(item.Description));
default:
return new JProperty(item.Name);
}
}
return new JObject(lookup[parentId].Select(x => ToProperty(x)));
}
var output = ToJson(null);
var text = Newtonsoft.Json.JsonConvert.SerializeObject(output, Newtonsoft.Json.Formatting.Indented);
return text;
}
}
I run it like this:
List<Item> MyItems = new List<Item>()
{
new Item { Id = 1, ParentId = null, Name = "Name1", Description = "", Type = "" },
new Item { Id = 2, ParentId = 1, Name = "Name2", Description = "Description1", Type = "String" },
new Item { Id = 3, ParentId = 1, Name = "Name3", Description = "", Type = "Array" },
new Item { Id = 4, ParentId = 3, Name = "", Description = "ArrayItem1", Type = "" },
new Item { Id = 5, ParentId = 3, Name = "", Description = "ArrayItem2", Type = "" },
new Item { Id = 6, ParentId = 3, Name = "", Description = "ArrayItem3", Type = "" },
new Item { Id = 7, ParentId = null, Name = "Name4", Description = "5", Type = "Int" },
};
Console.WriteLine(MyItems.ToJson());
And I get this out:
{
"Name1": {
"Name2": "Description1",
"Name3": [
"ArrayItem1",
"ArrayItem2",
"ArrayItem3"
]
},
"Name4": 5
}

Consider missing object keys as null in JSON.net with JSON Path

My JSON looks like this sometime, which has name key missing in 2nd object. It may be any key in thousands of JSON file I read dynamically to extract fields required using JSON Path queries.
void Main()
{
string jsonText = #"[
{
'rank': 1,
'name': 'name 1',
'year': '1991'
},
{
'rank': 2,
'year': '1992'
},
{
'rank': 3,
'name': 'name 3',
'year': '1993'
}
]";
JToken json = JToken.Parse(jsonText);
List<Fields> fields = new List<Fields>
{
new Fields
{
name = "rank",
path = "$.[*].rank",
result = new List<string>()
},
new Fields
{
name = "name",
path = "$.[*].name",
result = new List<string>()
},
new Fields
{
name = "year",
path = "$.[*].year",
result = new List<string>()
}
};
foreach(var field in fields)
{
var result = json.SelectTokens(field.path);
foreach (var token in result)
{
if (token is JObject || token is JArray)
{
field.result.Add(token.ToString(Newtonsoft.Json.Formatting.None));
}
else
{
field.result.Add(token.ToString());
}
}
}
fields.Dump();
}
public class Fields
{
public string name { get; set; }
public string path { get; set; }
public List<string> result {get; set;}
}
Which results in below incorrect table as the "name 3" moved one row up on 2nd row. While it should be on 3rd row and the 2nd row should have null value.
Here is one way (Updated to fix trailing missing fields):
void Main()
{
string jsonText = #"[
{
'rank': 1,
'name': 'name 1',
'year': '1991'
},
{
'rank': 2,
'year': '1992'
},
{
'rank': 3,
'name': 'name 3',
}
]";
JToken json = JToken.Parse(jsonText);
List<Fields> fields = new List<Fields>
{
new Fields
{
name = "rank",
path = "$.[*].rank",
result = new List<string>()
},
new Fields
{
name = "name",
path = "$.[*].name",
result = new List<string>()
},
new Fields
{
name = "year",
path = "$.[*].year",
result = new List<string>()
}
};
var max = json.SelectTokens("$.[*]").Count().Dump();
foreach (var field in fields)
{
var result = json.SelectTokens(field.path);
var index = 0;
foreach (var token in result)
{
while(!token.Path.StartsWith($"[{index++}]"))
field.result.Add(null);
if (token is JObject || token is JArray)
{
field.result.Add(token.ToString(Newtonsoft.Json.Formatting.None));
}
else
{
field.result.Add(token.ToString());
}
}
field.result.AddRange(Enumerable.Range(0,max-result.Count()).Select(z=>(string)null));
}
fields.Dump();
}
public class Fields
{
public string name { get; set; }
public string path { get; set; }
public List<string> result { get; set; }
}

LINQ for getting list with occurrence more than 1

there is a List contains below class
public class DummyClass
{
public string name { get; set; }
public int value { get; set; }
public string status { get; set; }
}
The original list and expected output look like below
It should fulfills below condition:
If a name with status = A exists (e.g. A1 & A2) --> remove other peers which contains the same name & status = I (it is supposed that only one status=A for each name)
If a name contains many status=I but didn't contain item with status=A, then only display 1 item with that name
It can be implemented by few for loops and remove the duplicated peers literally, The question is rather or not it can be implemented by a LINQ
How about this:
var list = new[]
{
new DummyClass{ Name = "A1", Value=1, Status= "A" },
new DummyClass{ Name = "A1", Value=2, Status= "I" },
new DummyClass{ Name = "A2", Value=3, Status= "I" },
new DummyClass{ Name = "A2", Value=4, Status= "I" },
new DummyClass{ Name = "A2", Value=5, Status= "A" },
new DummyClass{ Name = "A3", Value=6, Status= "I" },
new DummyClass{ Name = "A3", Value=7, Status= "I" },
};
var aggregate = list
.OrderBy(item => item.Status)
.GroupBy(item => item.Name)
.Select(group => group.First());
foreach (var item in aggregate)
{
Console.WriteLine($"{item.Name} - {item.Value} - {item.Status}");
}
It will at first order by status ("A" comes before "I") and then group by the given name. Due to this, the first element of each group will be either the first "A" entry or if that missing the first "I" entry.
Output:
A1 - 1 - A
A2 - 5 - A
A3 - 6 - I
This is your LINQ to get expected collection:
collection.GroupBy(g => g.name)
.ToDictionary(d => d.Key, d => (d.OrderBy(or => or.status).First()));
collection is the collection of all rows from your table.
And here is code to check it:
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestConsoleApp
{
class DummyClass
{
public string name { get; set; }
public int value { get; set; }
public string status { get; set; }
}
class DummyClassCollection
{
private List<DummyClass> _collection;
public DummyClassCollection()
{
_collection = new List<DummyClass>();
FillUpCollection();
}
public void FillUpCollection()
{
_collection.Add(new DummyClass(){
name = "A1",
value = 1,
status = "A"
});
_collection.Add(new DummyClass(){
name = "A1",
value = 2,
status = "I"
});
_collection.Add(new DummyClass(){
name = "A2",
value = 3,
status = "I"
});
_collection.Add(new DummyClass(){
name = "A2",
value = 4,
status = "I"
});
_collection.Add(new DummyClass(){
name = "A2",
value = 5,
status = "A"
});
_collection.Add(new DummyClass(){
name = "A3",
value = 6,
status = "I"
});
_collection.Add(new DummyClass()
{
name = "A3",
value = 7,
status = "I"
});
}
public List<DummyClass> GetCollection()
{
return _collection;
}
}
class Program
{
static void Main(string[] args)
{
var dcc = new DummyClassCollection();
var collection = dcc.GetCollection();
var result = collection.GroupBy(g => g.name)
.ToDictionary(d => d.Key, d => (d.OrderBy(or => or.status).First()));
Console.ReadKey();
}
}
}

Can I use LINQ GroupBy to do this more cleanly?

In this contrived example, which closely resembles my real-world problem, I have a data set coming from an external source. Each record from the external source takes the following form:
[Classification] NVARCHAR(32),
[Rank] INT,
[Data] NVARCHAR(1024)
I am looking to build an object where the Rank and Data are patched into a single instance of a response object that contains list properties for the three hard-coded Classification values, ordered by Rank.
I have something that works, but I can't help but think that it could be done better. This is what I have:
public static void Main()
{
IEnumerable<GroupingTestRecord> records = new List<GroupingTestRecord>
{
new GroupingTestRecord { Classification = "A", Rank = 1, Data = "A1" },
new GroupingTestRecord { Classification = "A", Rank = 2, Data = "A2" },
new GroupingTestRecord { Classification = "A", Rank = 3, Data = "A3" },
new GroupingTestRecord { Classification = "B", Rank = 1, Data = "B1" },
new GroupingTestRecord { Classification = "B", Rank = 2, Data = "B2" },
new GroupingTestRecord { Classification = "B", Rank = 3, Data = "B3" },
new GroupingTestRecord { Classification = "C", Rank = 1, Data = "C1" },
new GroupingTestRecord { Classification = "C", Rank = 2, Data = "C2" },
new GroupingTestRecord { Classification = "C", Rank = 3, Data = "C3" },
};
GroupTestResult r = new GroupTestResult
{
A = records.Where(i => i.Classification == "A").Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank),
B = records.Where(i => i.Classification == "B").Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank),
C = records.Where(i => i.Classification == "C").Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank),
};
The source record DTO:
public class GroupingTestRecord
{
public string Classification { get; set; }
public int? Rank { get; set; }
public string Data { get; set; }
}
The destination single class:
public class GroupTestResult
{
public IEnumerable<GroupTestResultItem> A { get; set; }
public IEnumerable<GroupTestResultItem> B { get; set; }
public IEnumerable<GroupTestResultItem> C { get; set; }
}
The distination child class:
public class GroupTestResultItem
{
public int? Rank { get; set; }
public string Data { get; set; }
}
Ouput
{
"A":[
{
"Rank":1,
"Data":"A1"
},
{
"Rank":2,
"Data":"A2"
},
{
"Rank":3,
"Data":"A3"
}
],
"B":[
{
"Rank":1,
"Data":"B1"
},
{
"Rank":2,
"Data":"B2"
},
{
"Rank":3,
"Data":"B3"
}
],
"C":[
{
"Rank":1,
"Data":"C1"
},
{
"Rank":2,
"Data":"C2"
},
{
"Rank":3,
"Data":"C3"
}
]
}
Fiddle
Is there a better way to achieve my goal here?
The same JSON output was achieved using GroupBy first on the Classification and applying ToDictionary on the resulting IGrouping<string, GroupingTestRecord>.Key
var r = records
.GroupBy(_ => _.Classification)
.ToDictionary(
k => k.Key,
v => v.Select(j => new GroupTestResultItem { Rank = j.Rank, Data = j.Data, }).OrderBy(k => k.Rank).ToArray()
);
var json = JsonConvert.SerializeObject(r);
Console.WriteLine(json);
which should easily deserialize to the destination single class (for example on a client)
var result = JsonConvert.DeserializeObject<GroupTestResult>(json);
is it possible to get the top level result into a GroupTestResult object?
Build the result from the dictionary
var result = new GroupTestResult {
A = r.ContainsKey("A") ? r["A"] : Enumerable.Empty<GroupTestResultItem>();,
B = r.ContainsKey("B") ? r["B"] : Enumerable.Empty<GroupTestResultItem>();,
C = r.ContainsKey("C") ? r["C"] : Enumerable.Empty<GroupTestResultItem>();,
};
Or this
var result = records.GroupBy(x => x.Classification)
.ToDictionary(x => x.Key, x => x.Select(y => new {y.Rank, y.Data})
.OrderBy(y => y.Rank));
Console.WriteLine(JsonConvert.SerializeObject(result));
Full Demo Here

How to get same results from select as foreach when using GroupBy()

I would like to be able to attain the same results that I can get by using foreach on a grouping when using the select method and an anonymous method.
public class ExportData
{
public int Id { get; set; }
public string Colour { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Money { get; set; }
}
public class ExportDataDictionary
{
public IDictionary<string, object> ColumnData { get; set; } = new Dictionary<string, object>();
}
Given the two classes above as an example.
I create some data..
var dataCollection = new List<ExportData>
{
new ExportData { Name = "Name1", Age = 1, Colour = "Blue", Id = 1, Money = 10 },
new ExportData { Name = "Name1", Age = 2, Colour = "Red", Id = 2, Money = 20 },
new ExportData { Name = "Name1", Age = 2, Colour = "Green", Id = 3, Money = 30 },
new ExportData { Name = "Name2", Age = 1, Colour = "Yellow", Id = 4, Money = 40 },
new ExportData { Name = "Name3", Age = 2, Colour = "Blue", Id = 5, Money = 50 },
new ExportData { Name = "Name4", Age = 3, Colour = "Blue", Id = 6, Money = 10 }
};
Next I group this data by, for example, two properties as follows..
var dataGrouping = dataCollection.GroupBy(g => new { g.Name, g.Age });
I then create a list of ExportDataDictionaries and foreach through each group in the grouping, creating a new ExportDataDictionary each time and adding both of the keys to the dictionary.
var data = new List<ExportDataDictionary>();
foreach (var grouping in dataGrouping)
{
var datadictionary = new ExportDataDictionary();
datadictionary.ColumnData.Add("NAME", grouping.Key.Name);
datadictionary.ColumnData.Add("AGE", grouping.Key.Age);
data.Add(datadictionary);
}
The result is a collection of 5 ExportDataDictionaries with 2 Columns in each one that contain the pair of keys that correspond to each of the groupings.
My attempt to achieve the same with the Select method is shown below.
var data2 = new List<ExportDataDictionary>();
var mydata = dataGrouping.Select(d =>
{
var datadictionary = new ExportDataDictionary();
datadictionary.ColumnData.Add("NAME", d.Key.Name);
datadictionary.ColumnData.Add("AGE", d.Key.Age);
data2.Add(datadictionary);
return data2;
});
The result is of the type:
mydata = {System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Linq.IGrouping<<>f__AnonymousType0<string, int>, ConsoleApp2.Program.ExportData>, System.Collections.Generic.List<ConsoleApp2.Program.ExportDataDictionary>>}
and it contains 5 items and each item contains 10 dictionaries. The 5 dictionaries that I expect are there with the same values as when using foreach but then there are 2 copies of each. I believe that this must be because it is creating the dictionaries for both of the keys used in the grouping. So, I am wondering how to only do this for one of the keys or just each group in the collection?
The requirement is that mydata should contain the same result as obtained by foreach in data variable
Any help much appreciated :)
Just Add .ToList() at the end of your last statement remove the data2.Add(datadictionary); statement and only return the datadictionary return datadictionary; like this
var mydata = dataGrouping.Select(d =>
{
var datadictionary = new ExportDataDictionary();
datadictionary.ColumnData.Add("NAME", d.Key.Name);
datadictionary.ColumnData.Add("AGE", d.Key.Age);
return datadictionary;
}).ToList();
I have run your code and checked and saw that mydata contains 5 items, and each item contains 2 ColumnData members.
Actually, your Linq query is only executed when you call the .ToList() function

Categories