<T> padding list in foreach loop after pattern matching
I am new to C # programming and need some help.
I am trying to assign the values ββthat I collected from the JSON feed to my own type, a class in which I defined certain fields (properties) for inputting JSON elements, plus elements that are obtained from the RegEx template matching process. This will then allow me to access the object using LINQ, as I use List to store my objects.
In my code there is a foreach loop that loops for every match that my RegEx method finds. I'm only interested in parts of the JSON channel where there is a match.
So my own specific class is this:
//simple field definition class public class TwitterCollection { public string origURL { get; set; } public string txtDesc { get; set; } public string imgURL { get; set; } public string userName { get; set; } public string createdAt { get; set; } } And then I want to populate the List in a RegEx Matches loop:
foreach (Match match in matches) { GroupCollection groups = match.Groups; var tc = new List<TwitterCollection>() { origURL = groups[0].Value.ToString(), txtDesc = res.text, imgUrl = res.profile_image_url, userName = res.from_user_id, createdAt = res.created_at, }; } Then the code will continue to extract and sort the results through Linq for objects. But the compiler will not actually let me create my var tc = new List<TwitterCollection>() , because: "System.Collections.Generic.List" does not contain a definition for "origURL" ... although I defined it.
It doesn't throw an error if I just write new TwitterCollection , but then how can I refer to this in my Linq expression later?
Please, help!
You need to instantiate the list outside the loop:
var list = new List<TwitterCollection>(); foreach (Match match in matches) { GroupCollection groups = match.Groups; var tc = new TwitterCollection { origURL = groups[0].Value.ToString(), txtDesc = res.text, imgUrl = res.profile_image_url, userName = res.from_user_id, createdAt = res.created_at, }; list.Add(tc); } You are currently trying to create a new list for each item. The actual compilation error is that your object initializer is for the TwitterCollection object, not a list of them, but in any case there is no fixed point with this logic flaw.
The problem is that you are trying to use the object initializer for the TwitterCollection object, but you are applying it to the List<TwitterCollection> . Instead, you should create a list and call Add instead of recreating it each time.
var list = new List<TwitterCollection>(); foreach (Match match in matches) { GroupCollection groups = match.Groups; var tc = new TwitterCollection() { origURL = groups[0].Value.ToString(), txtDesc = res.text, imgUrl = res.profile_image_url, userName = res.from_user_id, createdAt = res.created_at, }; list.Add(tc); } Or if you only need a LINQ query
var list = matches .Cast<Match>() .Select(x => new TwitterCollection() { origURL = x.Groups[0].Value.ToString(), txtDesc = res.text, imgUrl = res.profile_image_url, userName = res.from_user_id, createdAt = res.created_at } ) .ToList(); Off-topic, but since you mentioned that you are new to C #, I thought I should mention that you should follow Microsoft's naming guidelines:
http://msdn.microsoft.com/en-us/library/fzcth91k%28VS.71%29.aspx
Your class declaration will be as follows:
public class TwitterCollection { public string OrignalUrl { get; set; } public string TextDescription { get; set; } public string ImageUrl { get; set; } public string UserName { get; set; } public string CreatedAt { get; set; } } Another thing not mentioned in the link is that in most cases the Hungarian notation (txtDesc, for example) should be used only in a few cases. Also, consider not using abbreviations unless he has accepted the nomenclature (e.g. Url), since usually there is no cost associated with using the full word, not abbreviation.
Hope this is helpful!