0

Following is the sample code, and am looking to send new values dynamically every time in the select new statement below, Trying to add new object dynamically in the select new statement below, currently am passing hard coded values but how can I send dynamic values and keep adding to existing values?

public static void Main(string[] args)
{

   // Post p = new Post();
    List<Post> posts = Program.GetPosts();
    // foreach(posting in posts)
    JObject o = JObject.FromObject(new
    {
        channel = new
        {
            title = "James Newton-King",
            link = "http://james.newtonking.com",
            description = "James Newton-King's blog.",
            item =
    from p in posts
    orderby p.title
    select new                              //here 
    {
        title = p.title,
        description = p.description,
        link = p.link,
        category = p.Item
    }
        }
    });

    Console.WriteLine(o);

}


public static List<Post> GetPosts()
    {
        Post c = new Post();
        List<Post> ch = new List<Post>();
        c.title = "hello title";
        c.link = "link";
        c.description = "des";
        c.Item = "Item";
        ch.Add(c);

        Post p = new Post();
        c.title = "hello title1";
        c.link = "link1";
        c.description = "des1";
        c.Item = "Item1";
        ch.Add(p);

        return ch;
    }
}

class Channel
{
   public string title;
    public string link;
    public string description;
    public string Item;
    public string name;


}
Andronicus
  • 25,419
  • 17
  • 47
  • 88
123456
  • 27
  • 4
  • https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object Use a dynamic type – Train Jan 24 '20 at 20:16
  • Not sure I understand your problem. If you want to add all the values of `Post` to the array, why not just do `select p`? Or just do `item = posts.OrderBy(p => p.title)`, see https://dotnetfiddle.net/LyuNTr. I don't see a need for an intermediate anonymous type at all if you want to add all the properties of the current type. – dbc Jan 24 '20 at 20:19
  • @dbc if we look at the GetPosts method, they are hard coded values, I might need to create more objects to add, i want to read new values dynamically from Getposts() , which is hardcoded but will update with API that gives different outputs, and need to append that to existing JObject – 123456 Jan 24 '20 at 21:22

0 Answers0