C# 如何在我的LINQ STANTION中使用.ForEach?

C# 如何在我的LINQ STANTION中使用.ForEach?,c#,xml,linq,C#,Xml,Linq,我试图迭代OrderItems列表,并将结果与我的Order元素相关联。在这个论坛的帮助下,当我的Order元素下只有一个OrderItem时,我能够使关联“正确”工作。但当我的Order元素下有多个OrderItems时,我无法开始工作 我看过很多显示.ForEach(x=>语法的线程,也看到了ToList()的建议,但我没有发现任何与我正在做的事情足够相似的东西来确定我需要做什么。我非常确定我需要在我的 OrderItems = new List<OrderItems>() 以

我试图迭代OrderItems列表,并将结果与我的Order元素相关联。在这个论坛的帮助下,当我的Order元素下只有一个OrderItem时,我能够使关联“正确”工作。但当我的Order元素下有多个OrderItems时,我无法开始工作

我看过很多显示.ForEach(x=>语法的线程,也看到了ToList()的建议,但我没有发现任何与我正在做的事情足够相似的东西来确定我需要做什么。我非常确定我需要在我的

OrderItems = new List<OrderItems>()
以上是代码中Console.WriteLines的结果

    static void Main()
    {
        XDocument document = XDocument.Parse(GetXml());

        var query = from el in document.Root.Elements("Order")
                    select new Orders
                    {
                        Id = (int)el.Element("Id"),
                        OrderDate = (DateTime)el.Element("OrderDate"),
                        OrderTotal = (Decimal)el.Element("OrderTotal"),

                        OrderItems = new List<OrderItems>()
                        {
                            new OrderItems()
                            { 
                                ProductName = (string)el.Element("OrderItems").Element("OrderItem").Element("ProductName"), 
                                Price = (Decimal)el.Element("OrderItems").Element("OrderItem").Element("Price"),
                                Quantity = (int)el.Element("OrderItems").Element("OrderItem").Element("Quantity")
                            }
                        }
                    };

        foreach (var cc in query)
        {
            Console.WriteLine(String.Format("ID: {0} OrderDate: {1} OrderTotal: {2}"
                , cc.Id
                , cc.OrderDate
                , cc.OrderTotal));
            foreach (var xx in cc.OrderItems)
            {
                Console.WriteLine("\tProductName: " + cc.OrderItems[0].ProductName);
                Console.WriteLine("\tPrice: " + cc.OrderItems[0].Price);
                Console.WriteLine("\tQuantity: " + cc.OrderItems[0].Quantity);
            }
        }

        Console.ReadLine();

    }

    public static String GetXml()
    {
        return @"<?xml version='1.0' encoding='utf-8' ?>
        <OrderXml>
          <Order>
            <Id>1</Id>
            <OrderDate>7/7/2014</OrderDate>
            <OrderTotal>12.99</OrderTotal>
            <OrderItems>
              <OrderItem>
                <ProductName>Silver Widgets</ProductName>
                <Price>12.99</Price>
                <Quantity>1</Quantity>
              </OrderItem>
            </OrderItems>
          </Order>
          <Order>
            <Id>2</Id>
            <OrderDate>7/7/2014</OrderDate>
            <OrderTotal>20.00</OrderTotal>
            <OrderItems>
              <OrderItem>
                <ProductName>Gold Widgets</ProductName>
                <Price>10.00</Price>
                <Quantity>1</Quantity>
              </OrderItem>
              <OrderItem>
                <ProductName>Blue Widgets</ProductName>
                <Price>10.00</Price>
                <Quantity>1</Quantity>
              </OrderItem>
            </OrderItems>
          </Order>
        </OrderXml>";
    }
static void Main()
{
XDocument document=XDocument.Parse(GetXml());
var query=来自document.Root.Elements(“订单”)中的el
选择新订单
{
Id=(int)el.Element(“Id”),
OrderDate=(DateTime)el.Element(“OrderDate”),
OrderTotal=(十进制)el.Element(“OrderTotal”),
OrderItems=新列表()
{
新订单项()
{ 
ProductName=(字符串)el.Element(“OrderItems”).Element(“OrderItem”).Element(“ProductName”),
价格=(十进制)el.Element(“OrderItems”).Element(“OrderItem”).Element(“价格”),
数量=(int)el.Element(“OrderItems”).Element(“OrderItem”).Element(“数量”)
}
}
};
foreach(查询中的var cc)
{
Console.WriteLine(String.Format(“ID:{0}OrderDate:{1}OrderTotal:{2}”
,cc.Id
,cc.OrderDate
,抄送订单总数);
foreach(cc.OrderItems中的变量xx)
{
Console.WriteLine(“\tProductName:+cc.OrderItems[0].ProductName”);
Console.WriteLine(“\t价格:+cc.OrderItems[0].Price”);
Console.WriteLine(“\tQuantity:+cc.OrderItems[0]。数量);
}
}
Console.ReadLine();
}
公共静态字符串GetXml()
{
返回@”
1.
7/7/2014
12.99
银色小部件
12.99
1.
2.
7/7/2014
20
黄金饰品
10
1.
蓝色小部件
10
1.
";
}

向LINQ添加一个
ForEach
操作符是一个常见的用户扩展,但默认情况下它不在LINQ中

通常的方法是贪婪地迭代调用不返回结果的委托的每个值


虽然迭代类似,但这与
foreach
关键字没有其他关系。

向LINQ添加
foreach
“运算符”是常见的用户扩展,但默认情况下它不在LINQ中

通常的方法是贪婪地迭代调用不返回结果的委托的每个值


虽然迭代类似,但这与
foreach
关键字没有其他关系。

向LINQ添加
foreach
“运算符”是常见的用户扩展,但默认情况下它不在LINQ中

通常的方法是贪婪地迭代调用不返回结果的委托的每个值


虽然迭代类似,但这与
foreach
关键字没有其他关系。

向LINQ添加
foreach
“运算符”是常见的用户扩展,但默认情况下它不在LINQ中

通常的方法是贪婪地迭代调用不返回结果的委托的每个值


虽然迭代是类似的,但这与foreach关键字没有其他关系。

微软决定不实施

但您可以实现自己的:

public static class Extensions
{
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (action == null) throw new ArgumentNullException("action");

        foreach (T item in source)
        {
            action(item);
        }
    }
}
公共静态类扩展
{
公共静态void ForEach(此IEnumerable源、操作)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
如果(action==null)抛出新的ArgumentNullException(“action”);
foreach(源中的T项)
{
行动(项目);
}
}
}
工作起来很有魅力


另外,您还需要执行@Servy的其他建议。

Microsoft已决定不执行

但您可以实现自己的:

public static class Extensions
{
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (action == null) throw new ArgumentNullException("action");

        foreach (T item in source)
        {
            action(item);
        }
    }
}
公共静态类扩展
{
公共静态void ForEach(此IEnumerable源、操作)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
如果(action==null)抛出新的ArgumentNullException(“action”);
foreach(源中的T项)
{
行动(项目);
}
}
}
工作起来很有魅力


另外,您还需要执行@Servy的其他建议。

Microsoft已决定不执行

但您可以实现自己的:

public static class Extensions
{
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (action == null) throw new ArgumentNullException("action");

        foreach (T item in source)
        {
            action(item);
        }
    }
}
公共静态类扩展
{
公共静态void ForEach(此IEnumerable源、操作)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
如果(action==null)抛出新的ArgumentNullException(“action”);
foreach(源中的T项)
{
行动(项目);
}
}
}
工作起来很有魅力


另外,您还需要执行@Servy的其他建议。

Microsoft已决定不执行

但您可以实现自己的:

public static class Extensions
{
    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (action == null) throw new ArgumentNullException("action");

        foreach (T item in source)
        {
            action(item);
        }
    }
}
公共静态类扩展
{
公共静态void ForEach(此IEnumerable源,Act)