List<Store> listStore = Session["store"] as List<Store>;//Store是个类
Store st = new Store(id, name, img);
//这里怎么判断listStore是否有st这个元素
如果没有就listStore.add(st)
MSDN里说listStore.Contains(st),但还是判断不出。
我用的是2.0。
Store st = new Store(id, name, img);
//这里怎么判断listStore是否有st这个元素
如果没有就listStore.add(st)
MSDN里说listStore.Contains(st),但还是判断不出。
我用的是2.0。
bool blnIsHave=false
foreach(Store objItem in listStore)
{
if(objItem.id==st.id) //判断是否相同的条件 或者自己定义
{
blnIsHave=true;
break;
}
}
if(!blnIsHave)
{
listStore.Add(st);
}
你是不是根据id还是什么的判断以免重复插入啊,你可以用Find方法,这方法参数是个委托,你可以自定义判断重复的条件。
public static bool Contains<TSource>(
this IEnumerable<TSource> source,
TSource value,
IEqualityComparer<TSource> comparer
)2.0下比较傻瓜的方法是循环List<Store>中每个元素,比较每个元素中的成员变量及属性是否相等。自己写一个方法包装下,看起来就简洁一些。
if(listStore.Contains(st))
{
//包含
}
else
{
//不包含
}