停止列表中的重复
本文关键字:列表 | 更新日期: 2023-09-27 18:11:13
我有一个网格,里面有几个下拉列表,我想要的是,当用户插入新数据时,检查列表中是否已经有相同的数据。
我的代码是这样的
if(ViewState["_priceSystems"]!=null)
_priceSystems=ViewState["_priceSystems"] as TList<PriceSystemItems>;
bool _isTrue=
PriceSystemExist(
_priceSystemItems.PricePlanId,
_priceSystemItems.SurchargePlanId,
_priceSystemItems.NoMatchAltPlanId);
if(_isTrue==false) {
_priceSystems.Add(_priceSystemItems);
}
这里我在_priceSystems列表中添加值<>和下面的代码,我正在检查值是否存在于列表
public bool PriceSystemExist(
int PricePlanId, int SurchagePlanId, int _noPlaneId) {
bool isExits=false;
if(ViewState["_priceSystems"]!=null)
_priceSystems=ViewState["_priceSystems"] as TList<PriceSystemItems>;
try {
if(_priceSystems!=null) {
foreach(PriceSystemItems item in _priceSystems) {
if(
item.PricePlanId==_priceSystemItems.PriceSystemId
&&
item.ServiceTypeId==_priceSystemItems.ServiceTypeId
&&
item.NoMatchAltPlanId==_priceSystemItems.NoMatchAltPlanId) {
isExits=true;
}
}
}
}
catch(Exception ex) {
}
return isExits;
}
我不明白我在foreach
循环中检查值做错了什么。
将比较方法更改为
public bool PriceSystemExist(int PricePlanId, int SurchagePlanId, int _noPlaneId)
{
bool isExits = false;
if (ViewState["_priceSystems"] != null)
_priceSystems = ViewState["_priceSystems"] as TList<PriceSystemItems>;
try
{
if(_priceSystems != null)
{
foreach (PriceSystemItems item in _priceSystems)
{
if (item.PricePlanId == PricePlanId && item.ServiceTypeId == SurchagePlanId && item.NoMatchAltPlanId == _noPlaneId)
{
isExits = true;
}
}
}
}
catch (Exception ex)
{
}
return isExits;
}
您没有在PriceSystemExist(int PricePlanId, int SurchagePlanId, int _noPlaneId)
方法中使用参数。
PricePlanId
SurchagePlanId
_noPlaneId
代替
_priceSystemItems.PriceSystemId
_priceSystemItems.ServiceTypeId
_priceSystemItems.NoMatchAltPlanId
foreach (PriceSystemItems item in _priceSystems)
{
if (item.PricePlanId == PricePlanId &&
item.ServiceTypeId == SurchagePlanId &&
item.NoMatchAltPlanId == _noPlaneId)
{
isExits = true;
}
}
你不想把_前缀混在一起