I am facing an issue while writing query to make a group by on inner list data to filter the outer list.
I have a collection structure like
"products"
{
"id": "97",
"name": "YI1",
"projects": [
{
"id": "92",
"name": "MUM",
"branches": [
{
"id": "62",
"name": "ON Service",
"geographyid": "84",
"geographyname": "North America",
"countryid": "52",
"countryname": "Canada"
}
],
"customers": [
{
"id": "80",
"name": "HEALTH SCIENCES"
}
]
}
],
},
"products"
{
"id": "96",
"name": "YI2",
"projects": [
{
"id": "94",
"name": "HHS",
"branches": [
{
"id": "64",
"name": "Hamilton ON Service",
"geographyid": "44",
"geographyname": "Asia",
"countryid": "58",
"countryname": "China"
}
],
"customers": [
{
"id": "40",
"name": "SCIENCES"
}
]
}
],
]
}
I am trying to have a new collection which can return an output as below
"Geography"{
"geographyid": "44",
"geographyname": "Asia",
"Country"
{
"countryid": "58",
"countryname": "China",
"branches"
{
"id": "94",
"name": "HHS
"customers"
{
"id": "40",
"name": "SCIENCES"
"projects"
{
"id": "94",
"name": "HHS",
"products"
{
"id": "96",
"name": "YI2",
}
}
},
}
}
},
"Geography"{
"geographyid": "84",
"geographyname": "North America"
"Country"
{
"countryid": "52",
"countryname": "Canada"
"branches"
{
"id": "62",
"name": "ON Service",
"customers"
{
"id": "80",
"name": "HEALTH SCIENCES"
"projects"
{
"id": "92",
"name": "MUM",
"products"
{
"id": "97",
"name": "YI1",
}
}
},
}
}
}
I tried multiple options and also write below query but I am still not getting required result.
var treeGroup = siteList.SelectMany(a => a.projects.Select(b => new { A = a, B = b }).ToList()).ToList()
.GroupBy(ol => new { ol.B.geographyid, ol.B.geographyname })
.Select(gGroup => new TreeNodes
{
id = gGroup.Key.geographyid,
name = gGroup.Key.geographyname,
type = Costants.geographyTreeNode,
parentid = string.Empty,
children = gGroup
.GroupBy(ol => new { ol.B.countryid, ol.B.countryname })
.Select(cGroup => new TreeNodes
{
id = cGroup.Key.countryid,
name = cGroup.Key.countryname,
type = Costants.countryTreeNode,
parentid = gGroup.Key.geographyid,
children = cGroup
.GroupBy(ol => new { ol.B.id, ol.B.name })
.Select(sGroup => new TreeNodes
{
id = sGroup.Key.id,
name = sGroup.Key.name,
type = Costants.branchTreeNode,
parentid = cGroup.Key.countryid,
children = sGroup
.Select(ol => new TreeNodes { id = ol.A.id, name = ol.A.name, type = Costants.siteTreeNode, parentid = sGroup.Key.id, children = new List<TreeNodes>() })
.ToList()
})
.ToList()
})
.ToList()
})
.ToList();
I can use looping logic to get the result, but I want to avoid it and try something with linq or lmbda expression.
I am able to resolve the issue. I took an additional parameters for Customers and then used the same with selectmany function on branches
Related
I am trying to filter nested data but the catch is, the children I am also trying to filter are of a different type.
I have data that looks like this:
{
"value": [
{
"UserName": "scottketchum",
"FirstName": "Scott",
"LastName": "Ketchum",
"MiddleName": null,
"Gender": "Male",
"Age": null,
"Emails": [
"Scott#example.com"
],
"FavoriteFeature": "Feature1",
"Features": [],
"AddressInfo": [
{
"Address": "2817 Milton Dr.",
"City": {
"Name": "Albuquerque",
"CountryRegion": "United States",
"Region": "NM"
}
}
],
"HomeAddress": null
},
{
"UserName": "harryingram",
"FirstName": "Harry",
"LastName": "Ingram",
"MiddleName": null,
"Gender": "Male",
"Age": null,
"Emails": [
"Harry#example.com"
],
"FavoriteFeature": "Feature2",
"Features": [],
"AddressInfo": [
{
"Address": "123 Scott Ln.",
"City": {
"Name": "Nashville",
"CountryRegion": "United States",
"Region": "TN"
}
}
],
"HomeAddress": null
}
]
}
I need to be able to type the word "Scott" in my search field and return any person that has the name "Scott" or has an Address with the word "Scott" in it. So, ideally, the search would return both people.
void searchitemfromjson(string s)
{
var text = File.ReadAllText("D://jsontest.txt");
var jObject1 = JObject.Parse(text)["value"];
var searchs = new List<string>();
foreach (var item in jObject1)
{
var jObjitem = JObject.Parse(item.ToString());
IList<string> keys = jObjitem.Properties().Select(p => p.Name).ToList();
foreach (var k in keys)
{
if (k == "UserName")
{
if (jObjitem[k].ToString().Contains(s)) searchs.Add(jObjitem[k].ToString());
}
else if(k == "AddressInfo")
{
if(jObjitem["AddressInfo"][0]["Address"].ToString().Contains(s)) searchs.Add(jObjitem[k].ToString()); ;
}
}
}
}
I have a json file where I want to search for a device (ex. AEVL2020) and will return multiple results based on "registers". I tried to search on the net but didn't find any clue.
So I want an output like this:
"12":{
"user_id": "1",
"employee_id": "12",
"name": "Juan Dela Cruz",
"privilege": "0"
},
"32":{
"user_id": "2",
"employee_id": "32",
"name": "Pedro Dela Cruz",
"privilege": "0"
}
Here is my json:
{
"devices": {
"AEVL2020":{
"deviceSerialId": "AEVL2020"
},
"AEVL2021":{
"deviceSerialId": "AEVL2021"
}
},
"registers":{
"AEVL2020":{
"12":{
"user_id": "1",
"employee_id": "12",
"name": "Juan Dela Cruz",
"privilege": "0"
},
"32":{
"user_id": "2",
"employee_id": "32",
"name": "Pedro Dela Cruz",
"privilege": "0"
}
},
"AEVL2021":{
"29":{
"user_id": "1",
"employee_id": "29",
"name": "Maria Dela Cruz",
"privilege": "0"
},
"222":{
"user_id": "2",
"employee_id": "222",
"name": "Jay Dela Cruz",
"privilege": "0"
}
}
}
}
using Newtonsoft.Json
var json = JObject.Parse("{\"devices\":{\"AEVL2020\":{\"deviceSerialId\":\"AEVL2020\"},\"AEVL2021\":{\"deviceSerialId\":\"AEVL2021\"}},\"registers\":{\"AEVL2020\":{\"12\":{\"user_id\":\"1\",\"employee_id\":\"12\",\"name\":\"Juan Dela Cruz\",\"privilege\":\"0\"},\"32\":{\"user_id\":\"2\",\"employee_id\":\"32\",\"name\":\"Pedro Dela Cruz\",\"privilege\":\"0\"}},\"AEVL2021\":{\"29\":{\"user_id\":\"1\",\"employee_id\":\"29\",\"name\":\"Maria Dela Cruz\",\"privilege\":\"0\"},\"222\":{\"user_id\":\"2\",\"employee_id\":\"222\",\"name\":\"Jay Dela Cruz\",\"privilege\":\"0\"}}}}");
var devices = json.Value<JObject>("devices");
var registers = json.Value<JObject>("registers");
foreach (var device in devices)
{
Console.WriteLine($"Device {device.Key}:");
Console.WriteLine(registers[device.Value.Value<string>("deviceSerialId")]);
}
I have output like you want
Device AEVL2020:
{
"12": {
"user_id": "1",
"employee_id": "12",
"name": "Juan Dela Cruz",
"privilege": "0"
},
"32": {
"user_id": "2",
"employee_id": "32",
"name": "Pedro Dela Cruz",
"privilege": "0"
}
}
Device AEVL2021:
{
"29": {
"user_id": "1",
"employee_id": "29",
"name": "Maria Dela Cruz",
"privilege": "0"
},
"222": {
"user_id": "2",
"employee_id": "222",
"name": "Jay Dela Cruz",
"privilege": "0"
}
}
p.s.: probably you would read json from file, so replace the first line to
var json = JObject.Parse(File.ReadAllText("file.json"));
Another similar way:
var obj = JsonConvert.DeserializeObject(json) as JObject;
var reg = obj["registers"]["AEVL2020"];
Or using a loop:
foreach (var deviceName in obj["devices"].Children<JProperty>().Select(x => x.Name))
{
var reg = obj["registers"][deviceName];
}
For example I have a document below for collection = delivery:
{
"doc": [
{
"docid": "15",
"deliverynum": "123",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
},
{
"docid": "16",
"deliverynum": "456",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
},
{
"docid": "17",
"deliverynum": "999",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
}
],
"id": "123",
"cancelled": false
}
is it possible to do a search with "deliverynum" = 999 and the output would be like below?
{
"doc": [
{
"docid": "17",
"deliverynum": "999",
"text": "txxxxxx",
"date": "2019-07-18T12:37:58Z"
}
],
"id": "123",
"cancelled": false
}
or should I make another Collection just for the Doc part?
I am having trouble making a query in C# for this kind of scenario.
In Mongo shell you can use the $(projection) operator:
db.collection.find({ "doc.deliverynum": "999" }, { "doc.$": 1 })
Corresponding C# code can look like below:
var q = Builders<Model>.Filter.ElemMatch(x => x.doc, d => d.deliverynum == "999");
var p = Builders<Model>.Projection.ElemMatch(x => x.doc, d => d.deliverynum == "999");
var data = Col.Find(q).Project(p).ToList();
You can also use q = Builders<Model>.Filter.Empty if you want to get all documents even if the don't contain deliverynum =``999
I'm building a dashboard using ASP.Net MVC, Angular.js, SQL Server and Fusion charts. All my data for charting is stored in database and I'm getting them via stored procedure. Now I need to pass the results from stored procedure to Json/XML, only formats Fusion Charts supports. What would be the best method to convert this data:
Hour Input Output InTarget OutTarget
7 22314 18537 6500 4875
8 36395 29931 6500 4875
9 32661 28518 6500 4875
10 34895 29793 6500 4875
11 30300 26538 6500 4875
12 31011 26898 6500 4875
13 16363 13716 6500 4875
into this Json?
{
"chart": {
"caption": "Input and Output",
"numberprefix": "$",
"plotgradientcolor": "",
"bgcolor": "FFFFFF",
"showalternatehgridcolor": "0",
"divlinecolor": "CCCCCC",
"showvalues": "0",
"showcanvasborder": "0",
"canvasborderalpha": "0",
"canvasbordercolor": "CCCCCC",
"canvasborderthickness": "1",
"yaxismaxvalue": "30000",
"captionpadding": "30",
"yaxisvaluespadding": "15",
"legendshadow": "0",
"legendborderalpha": "0",
"palettecolors": "#f8bd19,#008ee4,#33bdda,#e44a00,#6baa01,#583e78",
"showplotborder": "0",
"showborder": "0"
},
"categories": [
{
"category": [
{
"label": "7"
},
{
"label": "8"
},
{
"label": "9"
},
{
"label": "10"
},
{
"label": "11"
},
{
"label": "12"
},
{
"label": "13"
}
]
}
],
"dataset": [
{
"seriesname": "Input",
"data": [
{
"value": "22314"
},
{
"value": "36395"
},
{
"value": "32661"
},
{
"value": "34895"
},
{
"value": "30300"
},
{
"value": "31011"
},
{
"value": "16363"
}
]
},
{
"seriesname": "Output",
"data": [
{
"value": "18537"
},
{
"value": "29931"
},
{
"value": "28518"
},
{
"value": "29793"
},
{
"value": "26538"
},
{
"value": "26898"
},
{
"value": "13716"
}
]
},
{
"seriesname": "InTarget",
"renderas": "Line",
"data": [
{
"value": "6500"
},
{
"value": "6500"
},
{
"value": "6500"
},
{
"value": "6500"
},
{
"value": "6500"
},
{
"value": "6500"
},
{
"value": "6500"
}
]
},
{
"seriesname": "OutTarget",
"renderas": "Line",
"data": [
{
"value": "4875"
},
{
"value": "4875"
},
{
"value": "4875"
},
{
"value": "4875"
},
{
"value": "4875"
},
{
"value": "4875"
},
{
"value": "4875"
}
]
}
]
}
What I'm thinking to do is:
stored procedure into datatable
put each column into separate array
convert array to Json in the format below
Is this going to be the best (performance) approach?
EDIT:
public Series[] GetGraphData(string sp)
{
var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].ConnectionString;
using (var da = new SqlDataAdapter("exec " + sp, connection))
{
var dt = new DataTable();
da.Fill(dt);
da.FillSchema(dt, SchemaType.Mapped);
Series[] arrSeries = new Series[dt.Columns.Count];
foreach(DataColumn dc in dt.Columns)
{
if (dc.Ordinal == 0)
{
//Category here
}
else
{
var strarr = dt.Rows.Cast<DataRow>().Select(row => row[dc.Ordinal]).ToList();
Series s = new Series()
{
seriesname = dc.ColumnName,
renderas = "Line",
data = strarr.Select(o => new SeriesValue { value = o.ToString() }).ToList()
};
arrSeries[dc.Ordinal] = s;
}
}
return arrSeries;
}
}
I would load all the data into a datatable as you said, then have a Series object:
class Series{
public string seriesname{get;set;}
public string renderas{get;set;}
public IList<SeriesValue> data{get;set;}
}
class SeriesValue{
public string value{get;set;}
}
and return an array of Series to the frontend, serialized as JSON. Then you have the dataset array already built and you don't need to do any other processing on it.
I expect the performance bottleneck to be in loading the data from the db and sending it to the client .. the actual conversion to json shouldn't matter in the grand scheme of things.
i'm working on a program that exports all user public data but the graph is making troubles,i made an app and got all the permissions (the extended profile properties permissions, and extended permissions) to generate the access token which i use to extract data, what confuses me is that some profiles extract the hole data some other don't even if it appears in the section.
example:
attempting to export me/ exported all the data i have sports education hometown etc..
attempting to export friend/ exported public data + sports and no education even tho he got education in the about section and it appears to public or friends
attempting to extract me/subscribers showed 12 subscribers which is right
attempting to extract friend/subscribers showed 12 of 20 even tho the subscribers appears as 20 in the subscribers section
i'm using the following command:
var res = JsonConvert.DeserializeObject<FacebookLikes>(fb.Get(txtUserName.Text + "/likes").ToString());
i hope you understand the idea, thanks for the help
okay here the problem with a live test:
i tried the following on the tool you stated:
blazzzin/?access_token
the result was:
{
"id": "100001748944712",
"name": "Ethan 'blaze' Parker",
"first_name": "Ethan",
"middle_name": "'blaze'",
"last_name": "Parker",
"link": "https://www.facebook.com/blazzzin",
"birthday": "02/20",
"hometown": {
"id": "102161913158207",
"name": "Delhi, India"
},
"quotes": "You're what you eat. Really? I am an Apple then.",
"sports": [
{
"id": "102173226491776",
"name": "Soccer"
},
{
"id": "112285278784684",
"name": "Badminton"
}
],
"favorite_teams": [
{
"id": "138570342846531",
"name": "WWE Nexus"
},
{
"id": "136759993012177",
"name": "Delhi Daredevils"
},
{
"id": "42884080673",
"name": "Uruguay"
},
{
"id": "365383271987",
"name": "Chelsea FC"
},
{
"id": "151170071595882",
"name": "Best of wwe"
},
{
"id": "166496596733768",
"name": "WWE World"
},
{
"id": "19221964237",
"name": "ECW"
},
{
"id": "31695961025",
"name": "Impact Wrestling"
},
{
"id": "5985827589",
"name": "Fnatic"
},
{
"id": "61630257716",
"name": "University of Cincinnati Bearcats"
},
{
"id": "44027083759",
"name": "Cincinnati Reds"
},
{
"id": "323348924370835",
"name": "Wrestling Memes"
},
{
"id": "127573517418762",
"name": "Veer Marathi"
},
{
"id": "208056762558521",
"name": "Team Razer"
},
{
"id": "228215007216138",
"name": "The awkward moment when you go to grab someone sexy and headbutt the mirror"
},
{
"id": "200933243299323",
"name": "If she's never seen Arsenal win a trophy, she's too young for you Bro"
},
{
"id": "189411184456673",
"name": "Sanfransisco Giants"
},
{
"id": "210487655659656",
"name": "Team Bring It vs Team Cenation"
},
{
"id": "146995142049876",
"name": "Like IF the First THNG You Do WheN You WAKE UP is ROll OVR &CHeCK UR Phone"
},
{
"id": "111507918902981",
"name": "• WWE Universe | Tunisian Page •"
}
],
"favorite_athletes": [
{
"id": "104023396299513",
"name": "Andrew Flintoff"
},
{
"id": "545498258812287",
"name": "Luke Wright"
},
{
"id": "111832475503333",
"name": "Michael Schumaker"
},
{
"id": "110393655686342",
"name": "Eddie Alvarez MMA"
},
{
"id": "112941715451427",
"name": "Maryse"
},
{
"id": "344128252278047",
"name": "Sachin Tendulkar"
},
{
"id": "179559748752616",
"name": "Maryse"
},
{
"id": "52911737290",
"name": "Doraemon"
},
{
"id": "14320933255",
"name": "Andrew Flintoff"
},
{
"id": "81221197163",
"name": "Cristiano Ronaldo"
},
{
"id": "136644946409767",
"name": "Mahmoud's charity run from Melbourne to Sydney"
},
{
"id": "42888741032",
"name": "R-Truth - WWE Universe"
},
{
"id": "203402113048458",
"name": "Abby Marie Johnson WBFF Bikini 2014 Competitor"
},
{
"id": "270759951686",
"name": "JASON DAVID FRANK - Official Fan Page"
},
{
"id": "110336188978264",
"name": "AJStyles.Org"
},
{
"id": "8707340185",
"name": "Edge - WWE Universe"
},
{
"id": "298344150293189",
"name": "Fandango - WWE"
},
{
"id": "195569370462754",
"name": "William Regal: A Tribute To An Underrated Legend"
},
{
"id": "173741199368967",
"name": "Steve Moriarty"
},
{
"id": "12714756642",
"name": "Fedor Emelianenko"
},
{
"id": "212481352152206",
"name": "Arkan Taha Fitness"
},
{
"id": "160505357316902",
"name": "Lexi Thompson"
},
{
"id": "10035000964",
"name": "The Great Khali - WWE Universe"
},
{
"id": "176063032413299",
"name": "Leo Messi"
},
{
"id": "65920772679",
"name": "Maria Sharapova"
},
{
"id": "226790007363705",
"name": "Divas - WWE UNiVERSE"
},
{
"id": "175535596516",
"name": "Sabine Lisicki"
},
{
"id": "415063578542561",
"name": "Eddie Guerrero"
},
{
"id": "157829220895967",
"name": "Kaitlyn - WWE Universe"
},
{
"id": "122046337858201",
"name": "WWE Maryse Ouellet"
},
{
"id": "185950250927",
"name": "Brock Lesnar"
},
{
"id": "8164128018",
"name": "Layla - WWE Universe"
},
{
"id": "110108465743414",
"name": "PUSH EVAN BOURNE"
},
{
"id": "115647961808394",
"name": "We love Evan Bourne"
},
{
"id": "105683519512165",
"name": "Sin Cara - WWE"
},
{
"id": "125557244171559",
"name": "Rikishi"
},
{
"id": "8457822873",
"name": "CM Punk - WWE Universe"
}
],
"inspirational_people": [
{
"id": "17774451468",
"name": "Mr. Bean"
},
{
"id": "9972312428",
"name": "The Miz - WWE Universe"
},
{
"id": "103107176396108",
"name": "CM Punk"
},
{
"id": "10392229299",
"name": "Gurbaksh Chahal"
}
],
"education": [
{
"school": {
"id": "424030284317485",
"name": "HarvardX"
},
"year": {
"id": "120960561375312",
"name": "2013"
},
"concentration": [
{
"id": "108146682539885",
"name": "Justice"
}
],
"type": "College"
},
{
"school": {
"id": "102052863223529",
"name": "TGC Animation and Multimedia"
},
"year": {
"id": "120960561375312",
"name": "2013"
},
"concentration": [
{
"id": "199849176695930",
"name": "Web Design & Development"
},
{
"id": "109803049037749",
"name": "Graphic Design"
}
],
"type": "College"
}
],
"gender": "male",
"relationship_status": "In a relationship",
"significant_other": {
"name": "Hailey Hayden",
"id": "100006853479540"
},
"website": "http://www.varunpuri.me
",
"locale": "en_US",
"languages": [
{
"id": "106059522759137",
"name": "English"
},
{
"id": "113301478683221",
"name": "American English"
},
{
"id": "112969428713061",
"name": "Hindi"
},
{
"id": "110343528993409",
"name": "Spanish"
},
{
"id": "105606752807048",
"name": "Punjabi"
},
{
"id": "107672419256005",
"name": "Dutch"
}
],
"updated_time": "2014-01-05T16:49:55+0000",
"username": "blazzzin"
}
i tried it on another friend and the friend have all the data public example work and education (i can see them in the about section)
khalil.bsaibes/?access_token
the result was:
{
"id": "806750229",
"name": "Khalil G. Bsaibes",
"first_name": "Khalil",
"middle_name": "G.",
"last_name": "Bsaibes",
"link": "https://www.facebook.com/khalil.bsaibes",
"location": {
"id": "106188806084417",
"name": "Beirut, Lebanon"
},
"gender": "male",
"locale": "en_US",
"updated_time": "2014-01-12T09:39:29+0000",
"username": "khalil.bsaibes"
}
so can you explain me what's happening and how to solve it?
What if the user whose information you are trying to access hasn't made some of his information public?
You can use the below tool to debug by putting the access token and your query and seeing what is the returned json. If it returns only 12 subscribers, then you should check the permission, if it returns 20 then recheck your code and parsing method..
https://developers.facebook.com/tools/explorer/