C# select data into the viewmodel using linq - c#
I have a viewmodel
public class CompanyDetailVM {
public string CompanyName { get; set; }
public IEnumerable<Comments> Comments { get; set; }
}
I want to retrieve all the data coming from XML, I cant figure out how to get list of comments too (Comments model consist of CommentText, DateTaken). My code looks like:
var model= new CompanyDetailVM
{
CompanyName = detail.Element("result").Element("name").Value,
Comments = new Models.Comments {
CommentText= detail.Element("result").Element("comment").Element("text"),
DateTaken = detail.Element("result").Element("comment").Element("date")
}
}
Error: Cannot implicitly convert type 'Models.Comments' to 'System.Collections.Generic.IEnumerable
I also dont think doing new Models.Comments is the right way. How do I fix the code correctly?
Currently, you assigned a Models.Comment object to a property expecting IEnumerable<Comments> type, which then trigger that conversion error. You can create IEnumerable<Comments> from XML using LINQ like so :
var model= new CompanyDetailVM
{
CompanyName = detail.Element("result").Element("name").Value,
Comments = from comment in detail.Element("result").Elements("comment")
select new Models.Comments
{
CommentText = (string)comment.Element("text"),
DateTaken = (DateTime)comment.Element("date")
}
}
Notice that I use Elements("comment") (with plural Elements) assuming that you may have multiple <comment> elements in the XML source.
If you just have a single Comment in your XML - as it looks like you do here - then the easiest is to do:
var model= new CompanyDetailVM
{
CompanyName = detail.Element("result").Element("name").Value,
Comments = new [] {
new Models.Comments {
CommentText= detail.Element("result").Element("comment").Element("text"),
DateTaken = detail.Element("result").Element("comment").Element("date")
}
}
}
(Note the shorthand array literal syntax - new [] { ... } for easily creating an array (which of course implements IEnumerable<>))
If your XML may contain multiple comments, e.g.
<result>
<name>Test</name>
<comment><text>One</text>...</comment>
<comment><text>Two</text>...</comment>
</result>
Then you might want to move towards using LinqToXML to transform all <comment> tags into Models.Comments objects.
Related
C# getting MongoDB BsonDocument to return as JSON
I asked a question a couple of days ago to collect data from MongoDB as a tree. MongoDB create an array within an array I am a newbie to MongoDB, but have used JSON quite substantially. I thought using a MongoDB to store my JSON would be a great benefit, but I am just experiencing immense frustration. I am using .NET 4.5.2 I have tried a number of ways to return the output from my aggregate query to my page. public JsonResult GetFolders() { IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("DataStore"); PipelineDefinition<BsonDocument, BsonDocument> treeDocs = new BsonDocument[] { // my query, which is a series of new BsonDocument } var documentGroup = collection.Aggregate(treeDocs).ToList(); // Here, I have tried to add it to a JsonResult Data, // as both documentGroup alone and documentGroup.ToJson() // Also, loop through and add it to a List and return as a JsonResult // Also, attempted to serialise, and even change the JsonWriterSettings. } When I look in the Immediate Window at documentGroup, it looks exactly like Json, but when I send to browser, it is an escaped string, with \" surrounding all my keys and values. I have attempted to create a model... public class FolderTree { public string id { get; set; } public string text { get; set; } public List<FolderTree> children { get; set; } } then loop through the documentGroup foreach(var docItem in documentGroup) { myDocs.Add(BsonSerializer.Deserialize<FolderTree>(docItem)); } but Bson complains that it cannot convert int to string. (I have to have text and id as a string, as some of the items are strings) How do I get my MongoDB data output as Json, and delivered to my browser as Json? Thanks for your assistance. ========= EDIT =========== I have attempted to follow this answer as suggested by Yong Shun below, https://stackoverflow.com/a/43220477/4541217 but this failed. I had issues, that the "id" was not all the way through the tree, so I changed the folder tree to be... public class FolderTree { //[BsonSerializer(typeof(FolderTreeObjectTypeSerializer))] //public string id { get; set; } [BsonSerializer(typeof(FolderTreeObjectTypeSerializer))] public string text { get; set; } public List<FolderTreeChildren> children { get; set; } } public class FolderTreeChildren { [BsonSerializer(typeof(FolderTreeObjectTypeSerializer))] public string text { get; set; } public List<FolderTreeChildren> children { get; set; } } Now, when I look at documentGroup, I see... [0]: {Plugins.Models.FolderTree} [1]: {Plugins.Models.FolderTree} To be fair to sbc in the comments, I have made so many changes to get this to work, that I can't remember the code I had that generated it. Because I could not send direct, my json result was handled as... JsonResult json = new JsonResult(); json.Data = documentGroup; //json.Data = JsonConvert.SerializeObject(documentGroup); json.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return json; Note, that I also tried to send it as... json.Data = documentGroup.ToJson(); json.Data = documentGroup.ToList(); json.Data = documentGroup.ToString(); all with varying failures. If I leave as documentGroup, I get {Current: null, WasFirstBatchEmpty: false, PostBatchResumeToken: null} If I do .ToJson(), I get "{ \"_t\" : \"AsyncCursor`1\" }" If I do .ToList(), I get what looks like Json in json.Data, but get an error of Unable to cast object of type 'MongoDB.Bson.BsonInt32' to type 'MongoDB.Bson.BsonBoolean'. If I do .ToString(), I get "MongoDB.Driver.Core.Operations.AsyncCursor`1[MongoDB.Bson.BsonDocument]" =========== EDIT 2 ================= As this way of extracting the data from MongoDB doesn't want to work, how else can I make it work? I am using C# MVC4. (.NET 4.5.2) I need to deliver json to the browser, hence why I am using a JsonResult return type. I need to use an aggregate to collect from MongoDB in the format I need it. My Newtonsoft.Json version is 11.0.2 My MongoDB.Driver is version 2.11.1 My method is the simplest it can be. What am I missing?
Filter c# object with JObject
Let me preface by saying I'm very new to C# development so if the solution seems obvious I apologize. I'm getting a JSON string back from a user and I need to filter a list of C# objects based on what the JSON string contains. The JSON can only have fields that my C# model has but I don't know what fields the JSON string will contain. My C# model looks something like this: public class Enrollment { public int Year { get; set; } public int NumEnrolls { get; set; } public int DaysIntoEnrollment { get; set; } public string Name { get; set; } } The JSON will have one or more of these properties with values to filter out. It could look like this: { "Year": ["2020", "2019"], "Name": ["CourseA", "CourseB"], "DaysIntoEnrollment": "20" } I need to filter my list of Enrollment objects based on the above JSON. So I would want the end result to have all Enrollment objects that don't contain a Year of 2020 or 2019 for example. I've gotten a filter to work with linq on a single property but my real model has much more properties that can be filtered and I'm looking for a compact solution that will work regardless of which properties are included in the JSON. This is what I have working public void GetFilteredData(string filters) { var enrollList = new List<Enrollments>(); // Pretend this contains a list of valid Enrollment data var json = JObject.Parse(filters); // filters string is in the json format from above var propsToFilter = from p in json["Year"] select p; var filtered = enrollList.Where(e => !propsToFilter.Contains(e.Year.ToString()))); } Is there a simple way to do this without manually going through each property like I did above?
Parse json file without key values
I'm trying to parse a json file with Json.net. The content of the json file is: [ [ "240521000", "37.46272", "25.32613", "0", "71", "90", "15", "2016-07-18T21:09:00" ], [ "237485000", "37.50118", "25.23968", "177", "211", "273", "8", "2015-09-18T21:08:00" ] ] I created the following code: WebClient wc = new WebClient(); string json = wc.DownloadString("data.json"); dynamic myObject = JsonConvert.DeserializeObject<dynamic>(json); foreach (string item in myObject[0]) { var x = item[0]; } How can I loop through all the individual items without having a key?
You probably just need two nested foreach statements. Try something like this: foreach (var items in myObject) { foreach (var item in items) { // do something } }
While diiN_'s answer answers the question, I don't think it's a good solution. Having had a look at the Marine Traffic API, it feels like they've made a poor JSON implementation, as the XML representation clearly has attribute names for the values. Their JSON should've been: {"positions": ["position": {"mmsi": "311029000", "lat": "37.48617", "long": "24.37233", ...}]} Because it isn't, we have a JSON string instead where it's a nested array, where the array is a two-dimensional array, and you'd have to hope the data model isn't changed to remove something, as you'd have to use an index to retrieve data. However, if you look at the XML available from the API, the attributes have names. I would suggest that you instead download the XML, and parse this into an object - a model, in ASP.NET - which is strongly typed, and can be used more easily in a View. Here's an example that I've got running. It uses XML parsing to first read the XML from the API, and then parse it to JSON, and finally an actual object. First, the model class (Position.cs) public sealed class Position { [JsonProperty("#MMSI")] public string MMSI { get; set; } [JsonProperty("#LAT")] public string Latitude { get; set; } [JsonProperty("#LON")] public string Longitude { get; set; } [JsonProperty("#SPEED")] public string Speed { get; set; } [JsonProperty("#HEADING")] public string Heading { get; set; } [JsonProperty("#COURSE")] public string Course { get; set; } [JsonProperty("#STATUS")] public string Status { get; set; } [JsonProperty("#TIMESTAMP")] public string TimeStamp { get; set; } } Next, the parsing logic: var client = new WebClient(); var xml = client.DownloadString("data.xml"); var doc = new XmlDocument(); doc.LoadXml(xml); var json = JsonConvert.SerializeXmlNode(doc); var positions = JObject.Parse(json).SelectToken("pos").SelectToken("row").ToObject<List<Position>>(); At the end of the parsing logic, you now have a list of Positions which you can pass to your view, and have it be strongly typed. As a brief example: // after you have the positions list return View(positions); Positions.cshtml #model List<Positions> <h2>Positions</h2> #foreach (var position in Model) { <p>#position.MMSI (#position.Latitude, #position.Longitude)</p> } I hope this is useful to you. If you have any questions, drop me a comment.
c# multi level json
I am trying to make a simple program that can automate price checking using data from json, however I haven't used json before and I'm not quite sure what I need to do get get the desired result. Effectively I'm trying to convert this PHP into c#. (http://www.sourcecodester.com/tutorials/php/8042/api-json-parsing-php.html) <?php $src = file_get_contents('https://www.g2a.com/marketplace/product/auctions/?id=256'); $json = json_decode($src, true); foreach ($json as $k=>$v) { if (is_array($v)) { foreach($v as $key=>$value) { if (is_array($value)) { foreach($value as $arKey => $arValue) { if ($arKey == 'p') { echo $arValue . '<br/>'; } } } } } } ?> I've tried a few things such as JsonConvert.DeserializeObject(webJson) and JObject.Parse(webJson), but I'm just not sure where I should be looking or how to go about doing it. I have the first part: internal static string GetWebContent(string #ID) { var wc = new WebClient(); string response = wc.DownloadString(#"https://www.g2a.com/marketplace/product/auctions/?id=" + #ID); return response; } A response looks like this: (https://www.g2a.com/marketplace/product/auctions/?id=27446) {"a":{"k_709942":{"c":"pl","tr":3451,"f":"\u00a324.71","fb":"\u00a327.18","ci":"565784","cname":"Marketplace User","p":"24.7098173","pb":"27.18431394","ep":"35.15","epb":"38.67","a":"709942","it":"game","t":"1","sl":"0","l":null,"lt":null,"x":0,"v":"all","so":0,"r":99},"k_763218":{"c":"pl","tr":1120,"f":"\u00a324.74","fb":"\u00a327.22","ci":"6533797","cname":"User #f36726dcd","p":"24.7449664","pb":"27.21946304","ep":"35.20","epb":"38.72","a":"763218","it":"game","t":"0","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":92},"k_799750":{"c":"pl","tr":559,"f":"\u00a324.78","fb":"\u00a327.26","ci":"6115711","cname":"Marketplace User","p":"24.7801155","pb":"27.26164196","ep":"35.25","epb":"38.78","a":"799750","it":"game","t":null,"sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":98},"k_722082":{"c":"gb","tr":49066,"f":"\u00a324.96","fb":"\u00a327.45","ci":"1047917","cname":"Marketplace User","p":"24.955861","pb":"27.4514471","ep":"35.50","epb":"39.05","a":"722082","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":98},"k_718113":{"c":"pl","tr":5852,"f":"\u00a324.96","fb":"\u00a327.45","ci":"226876","cname":"Marketplace User","p":"24.955861","pb":"27.4514471","ep":"35.50","epb":"39.05","a":"718113","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":98},"k_739155":{"c":"pl","tr":1208,"f":"\u00a325.43","fb":"\u00a327.98","ci":"6540559","cname":"User #1f948a6a66249","p":"25.43316468854","pb":"27.976481157394","ep":"35.30","epb":"38.83","a":"739155","it":"game","t":"0","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":89},"k_795049":{"c":"pl","tr":50420,"f":"\u00a325.86","fb":"\u00a328.45","ci":"2498689","cname":"Marketplace User","p":"25.86270778","pb":"28.44968154","ep":"36.79","epb":"40.47","a":"795049","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":99},"k_816132":{"c":"pl","tr":22,"f":"\u00a326.00","fb":"\u00a328.60","ci":"6208533","cname":"Marketplace User","p":"25.99627436","pb":"28.59730776","ep":"36.98","epb":"40.68","a":"816132","it":"game","t":"0","sl":"0","l":null,"lt":"0","x":0,"v":"retail","so":0,"r":0},"k_809925":{"c":"pl","tr":81021,"f":"\u00a326.00","fb":"\u00a328.60","ci":"407513","cname":"Marketplace User","p":"26.00330418","pb":"28.60433758","ep":"36.99","epb":"40.69","a":"809925","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"retail","so":0,"r":100},"k_815898":{"c":"cl","tr":1,"f":"\u00a326.01","fb":"\u00a328.61","ci":"7524623","cname":"Marketplace User","p":"26.010334","pb":"28.6113674","ep":"37.00","epb":"40.70","a":"815898","it":"game","t":null,"sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":0},"k_711901":{"c":"pl","tr":12194,"f":"\u00a326.51","fb":"\u00a329.16","ci":"286793","cname":"Marketplace User","p":"26.506689203722","pb":"29.158078610346","ep":"36.79","epb":"40.47","a":"711901","it":"game","t":"1","sl":"0","l":null,"lt":null,"x":0,"v":"all","so":0,"r":99},"k_748710":{"c":"pt","tr":66460,"f":"\u00a326.65","fb":"\u00a329.32","ci":"440288","cname":"Marketplace User","p":"26.650786454082","pb":"29.316585585742","ep":"36.99","epb":"40.69","a":"748710","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":100},"k_709464":{"c":"pl","tr":121341,"f":"\u00a327.02","fb":"\u00a329.72","ci":"1072530","cname":"User #3285e5f8dfcb2","p":"27.0182344425","pb":"29.72005788675","ep":"37.50","epb":"41.25","a":"709464","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"retail","so":0,"r":100},"k_709805":{"c":"pl","tr":11708,"f":"\u00a328.09","fb":"\u00a330.90","ci":"1043113","cname":"User #ca1965d0354a","p":"28.091758957682","pb":"30.901655339702","ep":"38.99","epb":"42.89","a":"709805","it":"game","t":"1","sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":100},"k_725839":{"c":"es","tr":1,"f":"\u00a331.99","fb":"\u00a335.18","ci":"7023396","cname":"Marketplace User","p":"31.985681","pb":"35.1842491","ep":"45.50","epb":"50.05","a":"725839","it":"game","t":null,"sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":0},"k_0":{"f":"\u00a332.33","fb":"\u00a335.56","p":"32.33014218","pb":"35.563156398","ep":"45.99","epb":"50.59","a":0,"c":"","rc":"","r":"","tr":0,"t":1,"so":0,"x":0,"n":"G2A.com"}},"w":0} Any help with will greatly appreciated. Thanks in advace
So Json is a way for two languages to pass objects to each other. It's just a way to encode an object, so the first part would be to create an object that matches the Json encoding. I was unable to see your example, so I will give you one of my own. {"Aggregates": [{"ColumnName": "Some Value", "AggregateFunction": "Some Other Value"}], "GroupBy": true} I would then create a class like this one. public class JsonAggregates { public List<Aggregate> Aggregates { get; set; } public string GroupBy { get; set; } } public class Aggregate { public string ColumnName {get; set;} public string AggregateFunction {get; set;} } You can then encode the Json to this new data type with the following code. using (Stream s = GenerateStreamFromString(json)) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonAggregates)); JsonAggregates aggregate = (JsonAggregates)serializer.ReadObject(s); }
Change the type of one of the objects property and sort it using LINQ
I want to sort the List where the objects properties are of string type. One of the property is a time of string type, and when i try to sort it sorts like below. 1:12, 13:24, 19:56, 2:15, 26:34, 8:42. Here the sorting is happening on string basis. Now i want to convert that sting to double (1.12, 13.24, 19.56, 2.15, 26.34, 8.42) and sort it. Then populate the data by replacing the '.' with ':'. I tried some thing like below, but still the sorting is happening on string basis. public class Model { public string Duration { get; set; } public string Dose { get; set; } } List<Model> lsModelData = new List<Model>(); //Added some model objects here // query for sorting the lsModelData by time. var sortedList = lsModelData.OrderBy(a => Convert.ToDouble(a.Duration.Replace(":", "."))); I am trying to replace the time ":" with "." and then convert that to double to perform the sort operation. Can any one please correct this statement to work this sorting properly.
If you want to sort data according to duration try this. its tested surely works for you. public class Models { public string Duration { get; set; } public string Dose { get; set; } } List<Models> lstModels = new List<Models>(); lstModels.Add(new Models { Duration = "101:12" }); lstModels.Add(new Models { Duration = "13:24" }); lstModels.Add(new Models { Duration = "19:56" }); List<Models> sortedList = (from models in lstModels select new Models { Dose = models.Dose, Duration = models.Duration.Replace(':','.')}) .ToList() .OrderBy(x=>Convert.ToDouble(x.Duration)) .ToList();
I'm not sure what you really want, but if you want to return only the duration, then select it after sort var sortedList = lsModelData.OrderBy(a => Convert.ToDouble(a.Duration.Replace(":", "."))).Select(a=> a.Duration).ToList(); or var sortedList = lsModelData..Select(a=> a.Duration).OrderBy(a => Convert.ToDouble(a.Replace(":", "."))).ToList();
In cases like this it works best to order by length and then by content: var sortedList = lsModelData.OrderBy(a => a.Duration.Length) .ThenBy(a => a.Duration) Converting database data before sorting (or filtering) always makes queries inefficient because indexes can't be used anymore.