score:1

Accepted answer

There is probably a better way, but Nix's answer helped me come up with this:

var data = from a in attributes                       
            select new
            {
                Id = a.Id,
                LabelText = a.LabelText,
                Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)                    
            };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);

score:2

You can do it but you need to use a group into to populate items.

var data = 
       from a in attributes
       from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
       group a by a.Id, a.LabelText into myGroup
       .DefaultIfEmpty(new DocClassAttributeFieldItem())
            select new
            {
                Id = a.Id,
                LabelText = a.LabelText,
                Items = myGroup.ToList()
            };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);

That was a shot from the hip, so let me know if it doesn't work for you.


Related Articles