如何执行使用LINQ或lambda更新?(C#,Asp.net,LINQ的,LAMBDA) [英] How to perform an update using Linq or Lambda?(C#, Asp.net,Linq , Lambda)

查看:344
本文介绍了如何执行使用LINQ或lambda更新?(C#,Asp.net,LINQ的,LAMBDA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能做一个更新Linq中

How can I do an update in Linq

我的code是

List<Cart> objNewCartItems = (List<Cart>)Session["CartItems"];

            if ((objNewCartItems != null) && (objNewCartItems.Count > 0))
            {
                for (int i = 0; i < dgShoppingCart.Rows.Count; i++)
                {
                    Cart c = new Cart();
                    TextBox t = (TextBox)dgShoppingCart.Rows[i].FindControl("txtQuantity");
                    c.Quantity = (t.Text == string.Empty? (int?)null: Convert.ToInt32(t.Text));

                    objNewCartItems[i].Quantity = c.Quantity;
                }
            }
            Session["CartItems"] = objNewCartItems;            
            Response.Redirect("ItemListing.aspx", false);

基本上在车收集,有一个项目称为将要填充的数量时,用户将填补

Basically in the Cart Collection, there is an item called an Quantity which will be populated when the user will fill that.

所以,我读了电网项目和更新数量的属性。

So I am reading the Grid item and updating the Quantity attribute.

但我相信一定有通过LINQ或λ做同样的一个更好的办法。

But I am sure that there must be a better way via linq or lambda to do the same.

请帮忙

在此先感谢

推荐答案

那么,你可以取代一个循环中的的foreach 循环,或者使用列表&LT; T&GT; .ForEach ,做内部相同,但最终LINQ是的查询的语法(因此Q),而不是一个突变的语法,并且它通常不是很有利的,试图通过匿名方法重新present更新(/λ)

Well, you could replace one for loop with a foreach loop, or maybe use the List<T>.ForEach that does the same internally, but ultimately LINQ is a query syntax (hence the Q), not a mutation syntax, and it isn't usually very beneficial to try to represent the updates via an anonymous method (/lambda).

需要注意的是.NET 4.0引入了防爆pression 节点,允许突变更好的支持,但C#编译器不添加任何支持他们,所以防爆pression lambda表达式仍仅限于查询(至少,通过语言)。

Note that .NET 4.0 introduces Expression nodes that allow better support for mutation, but the C# compiler doesn't add any support for them, so Expression lambdas are still limited to query (at least, via the language).

在短期:可能与你所拥有的坚持。您的可能的这里增加一些LINQ,但它会从根本上改变了code以同样的方式,它可以为某些查询,所以我看不到好处。

In short: maybe stick with what you have. You could add some LINQ here, but it would radically change the code in the same way that it can for some queries, so I don't see the benefit.

这篇关于如何执行使用LINQ或lambda更新?(C#,Asp.net,LINQ的,LAMBDA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆