English 中文(简体)
• 如何使用LINQ
原标题:How to populate List<T> inside .Select() using LINQ

我试图使用以下方法从XML的回复中填满IE amountable<Resort supplementier>。 LINQ Query: How do I populatione/create IList<PerPricing> PricingDetail within my LINQ query? 在目前情况下将有一个项目(PerPricing)(IList<PerPricing>)。 它需要成为未来选择的IList,并支持其他一些功能。 我的错误之处在于:

var resortPricing =
    xDoc.Elements("ResortPricingRS")
    .Elements("ResortPricing")
    .GroupBy(x => new
    {
        ID = x.Element(XName.Get("ResortId")).Value
    }
            )
    .Select(x => new ResortSupplier
    {
        SupplierCode = x.Key.ID,
        ResortProducts = x.Select(i => new Product
        {
            Code = i.Element("RoomCategory").Value,
            Description = i.Element("CategoryDesc").Value,
            Rating = (i.Elements("StarRatingCode").Any() ? i.Element("StarRatingCode").Value : ""),
            PricingDetail = { 
                            new PerPricing { 
                                    PricingType = "PerRoom",
                                    GroupNumber = 1 
                                    Price = decimal.Parse(i.Elements("TotalPrice").Any() ? i.Element("TotalPrice").Value : "0"),
                                    PricingError = (i.Elements("PricingError").Any() ? new Error { ErrorCode = i.Element("PricingError").Value } : null)
                                            } 
                            }
        }
                                ).ToList()
    }
            ).OrderBy(x => x.SupplierCode);

我的目标如下:

[Serializable]
public class ResortSupplier
{
    public string SupplierCode { get; set; }
    public IList<Product> ResortProducts { get; set; }
}

[Serializable]
public class Product
{
    public string Code { get; set; }
    public string Description { get; set; }
    public string Rating { get; set; }
    public IList<PerPricing> PricingDetail { get; set; }
}

[Serializable]
public class PerPricing
{
    public string PricingType { get; set; }
    public int GroupNumber { get; set; }
    public decimal? Price { get; set; }
    public Error PricingError { get; set; }
}
最佳回答

增加<代码>新[,如:

PricingDetail = new [] { 
                        new PerPricing { 
                                PricingType = "PerRoom",
                                GroupNumber = 1 
                                Price = decimal.Parse(i.Elements("TotalPrice").Any() ? i.Element("TotalPrice").Value : "0"),
                                PricingError = (i.Elements("PricingError").Any() ? new Error { ErrorCode = i.Element("PricingError").Value } : null)
                                        } 
                        }
    }
                            ).ToList()
问题回答

暂无回答




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签