开发者

Linq XML - create complex types

I have no firm idea how to word this question, so forgive me if I don't use the correct terminology.

I have a System.Xml.Linq.XElement el which contains an xml document. I want to query a part of the XElement and add user objects (Upload) to a generic list (UploadList held in a page property) like 开发者_如何学JAVAso:

this.UploadsList.Add((from el1 in el.Descendants("Uploads")
                                      select new Upload
                                      {
                                          Comments = el1.Element("Comments") != null ? el1.Element("Comments").Value : ""
                                          , 
                                          ***ConsentForImageUse.Text=el1.Element("SomeNode") != null ? el1.Element("SomeNode").Value : ""***
                                          ,
                                          DateImageProduced = el1.Element("DateImageProduced").Value != "" ? DateTime.Parse(el1.Element("DateImageProduced").Value) : DateTime.MinValue
                                          ,
                                          ImageTakenBy = el1.Element("ImageTakenBy") != null ? el1.Element("ImageTakenBy").Value : ""
                                      }).First<Upload>());

The compiler complains about the bit with the asterisks.

My Upload object has a property ConsentForImageUse which is itself a class with a couple of properties.

The error is Invalid initialiser member declarator. Does anyone know if I can create 'complex' objects using Linq like this like this?


Because of the way that object initializers are specified, you cannot set a property of a property in one. Instead, create an your ConsentForImageUse object as a let binding in your LINQ query, and assign that to the Upload object's ConsentForImageUse property. Without further knowledge of your precise object model, I've made some assumptions, but this ought to work for you.

var uploads = from el1 in el.Descendants("Uploads")
              let consent = new ConsentForImageUse() { Text = el1.Element("SomeNode") != null ? el1.Element("SomeNode").Value : string.Empty }
              select new Upload
              {
                  Comments = el1.Element("Comments") != null ? el1.Element("Comments").Value : string.Empty,
                  ConsentForImageUse = consent,
                  DateImageProduced = el1.Element("DateImageProduced") != null ? el1.Element("DateImageProduced").Value : string.Empty,
                  ImageTakenBy = el1.Element("ImageTakenBy") != null ? el1.Element("ImageTakenBy").Value : string.Empty
              };

list.Add(uploads.First());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜