C# 在会话变量中存储篮子的项目
我想存储购物篮的商品代码以及用户在会话数组中选择的数量 我不知道存储这些数据的最简单和最合适的数据类型是什么 我得到的印象是,我不能只在会话中存储我自己的对象 我希望能够再次提取这些值,以便将它们放入篮子中C# 在会话变量中存储篮子的项目,c#,asp.net,session,variables,shopping-cart,C#,Asp.net,Session,Variables,Shopping Cart,我想存储购物篮的商品代码以及用户在会话数组中选择的数量 我不知道存储这些数据的最简单和最合适的数据类型是什么 我得到的印象是,我不能只在会话中存储我自己的对象 我希望能够再次提取这些值,以便将它们放入篮子中 我尝试了List和ArrayList,但两种方法似乎都没有达到预期效果。您考虑过SortedDictionary对象吗?在这里找到参考资料。顺便说一句,如果您使用InProc或ASP.NET会话管理器服务,并且访问量很大,可能会遇到内存不足的问题,您确定要使用会话执行此操作吗。考虑使用SQL
我尝试了
List
和ArrayList
,但两种方法似乎都没有达到预期效果。您考虑过SortedDictionary对象吗?在这里找到参考资料。顺便说一句,如果您使用InProc或ASP.NET会话管理器服务,并且访问量很大,可能会遇到内存不足的问题,您确定要使用会话执行此操作吗。考虑使用SQL Server将此结构移动到DB或可能实现会话。在此处查看会话状态模式信息以及如何配置SQL Server状态管理。您是否考虑过SortedDictionary对象?在这里找到参考资料。顺便说一句,如果您使用InProc或ASP.NET会话管理器服务,并且访问量很大,可能会遇到内存不足的问题,您确定要使用会话执行此操作吗。考虑使用SQL Server将此结构移动到DB或可能实现会话。检查此处,了解会话状态模式信息以及如何配置SQL Server状态管理。创建包含BasketItems的列表
public class CartItem
{
public int ProductID {get;set;}
public int Quantity {get;set;}
}
List<CartItem> items = new List<CartItem>();
items.Add(new CartItem() { ProductID = 1, Quantity = 2 })
Session["cartItems"] = items;
to get it back again
if(Session["cartItems"] != null)
List<CartItem> items = (List<CartItem>)Session["cartItems"];
创建包含BasketItems的列表
public class CartItem
{
public int ProductID {get;set;}
public int Quantity {get;set;}
}
List<CartItem> items = new List<CartItem>();
items.Add(new CartItem() { ProductID = 1, Quantity = 2 })
Session["cartItems"] = items;
to get it back again
if(Session["cartItems"] != null)
List<CartItem> items = (List<CartItem>)Session["cartItems"];
我的印象是,我不能只在会话中存储自己的对象
你为什么会有这样的印象?您可以在会话中存储自己的对象。如果您需要一个web场并将会话存储在数据库中,请确保使用[Serializable]
标记您的对象
若要跟踪顺序,可以向对象添加另一个属性。我建议这样做:
[Serializable]
public class CartEntry
{
public string ProductCode { get; set; }
public int Quantity { get; set; }
public int Order { get; set; }
}
我的印象是,我不能只在会话中存储自己的对象
你为什么会有这样的印象?您可以在会话中存储自己的对象。如果您需要一个web场并将会话存储在数据库中,请确保使用[Serializable]
标记您的对象
若要跟踪顺序,可以向对象添加另一个属性。我建议这样做:
[Serializable]
public class CartEntry
{
public string ProductCode { get; set; }
public int Quantity { get; set; }
public int Order { get; set; }
}
您可以在会话中存储自己的自定义对象列表,只要该对象(及其任何对象依赖项)标记为
[Serializable]
。在这种情况下,您可能会:
[Serializable]
public class BasketItem
{
public string ProductCode { get; set; }
public int Quantity { get; set; }
}
然后只需将这些对象的集合存储在会话中,例如列表
更新:我想根据下面的评论进行澄清,因为这是正确的,
[Serializable]
如果会话存储在进程/内存中,则不需要。但是,如果对象存储在state server/SQL数据库中,则肯定需要使用它,因为该对象必须在站点和会话存储之间进行序列化/反序列化。只要该对象(及其任何对象依赖项)标记为[Serializable]
,您就可以在会话中存储自己的自定义对象列表。在这种情况下,您可能会:
[Serializable]
public class BasketItem
{
public string ProductCode { get; set; }
public int Quantity { get; set; }
}
然后只需将这些对象的集合存储在会话中,例如列表
更新:我想根据下面的评论进行澄清,因为这是正确的,
[Serializable]
如果会话存储在进程/内存中,则不需要。但是,如果对象存储在状态服务器/SQL数据库中,则肯定需要此选项,因为该对象必须在站点和会话存储之间序列化/反序列化。stackoverflow上还有一个线程描述了满足您需求的解决方案:
MSDN上还描述了整个过程和示例:
我还创建了一个示例来演示如何存储和恢复ArrayList对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Diagnostics;
public partial class SessionVarTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string item01 = "Item 1";
string item02 = "Item 2";
ArrayList MyArrayIn = new ArrayList();
MyArrayIn.Add(item01);
MyArrayIn.Add(item02);
Session["MyBasket"] = MyArrayIn;
ArrayList MyArrayOut = (ArrayList)Session["MyBasket"];
Debug.WriteLine("Content of ArrayList restored from session var");
Debug.WriteLine("ArrayList item 1: " + MyArrayOut[0]);
Debug.WriteLine("ArrayList item 2: " + MyArrayOut[1]);
}
}
stackoverflow上还有一个线程描述了满足您需求的解决方案: MSDN上还描述了整个过程和示例: 我还创建了一个示例来演示如何存储和恢复ArrayList对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Diagnostics;
public partial class SessionVarTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string item01 = "Item 1";
string item02 = "Item 2";
ArrayList MyArrayIn = new ArrayList();
MyArrayIn.Add(item01);
MyArrayIn.Add(item02);
Session["MyBasket"] = MyArrayIn;
ArrayList MyArrayOut = (ArrayList)Session["MyBasket"];
Debug.WriteLine("Content of ArrayList restored from session var");
Debug.WriteLine("ArrayList item 1: " + MyArrayOut[0]);
Debug.WriteLine("ArrayList item 2: " + MyArrayOut[1]);
}
}
您可以将自己的对象存储在sessionWell中,如果我可以存储自己的对象,这将使它变得更容易。这正是我想做的。我如何通过ProductID获取数量?@Coulton在linq行之后(var cartItem=(来自items中的ci,其中…),使用“cartItem.quantity”.cartItem是从linq select语句返回的对象。您可以将自己的对象存储在sessionWell中,如果我可以存储自己的对象,这将变得更加容易。这正是我想做的。我如何通过ProductID获取数量?@Coulton在linq行之后(var cartItem=(来自items where…)的ci),使用“cartItem.Quantity”.cartItem是从linq select语句返回的对象。作为一个简单的例子,您不需要使用
列表
,只需要使用字典
。字典已经是一个“列表”,它几乎是在C#中实现IEnumerable
的任何东西。您可以使用foreach枚举字典(dict.Keys中的var-key)
。作为一个简短的旁白,您不需要使用列表
,您只需要使用字典
。字典已经是一个“列表”,它几乎是用C#实现IEnumerable
的任何东西。您可以使用foreach(dict.Keys中的var-key)
枚举字典。添加[Serializable]不是必须添加的[Serializable]不是必须添加的