查找列表是否包含以逗号分隔的字符串中的任何项



我已经创建了一个包含食品类别,子类别,代码,食品名称(例如11,12,2345,'燕麦')的列表。现在在我的隐藏领域,有多个代码。1123、2345、5678。现在我想要得到的是列表中存在的代码计数。如。在上面的上下文中,假设在匹配的列表中只有2345存在,那么输出应该是1。如果匹配列表中存在1123,2345,则输出应为2。我试过像下面这样,但没有得到要求。输出。

public void GetCSWSTUCPAFreeRestrictBlkFood(string categoryId, string subCategoryId)
{
string rtnMsg = string.Empty;
int itmAlreadyExistsCnt = 0;
List<CurrentDietSummary> restrData = null;
restrData = GetPARestrictedFood();
var listCS = hfCheckedChildCS.Value.Split(',').ToList();
if (restrData != null && restrData.Count > 0)
itmAlreadyExistsCnt = restrData.Count(x => x.CategoryId == Convert.ToInt32(categoryId) 
&& x.SubCategoryId == Convert.ToInt32(subCategoryId)
&& x.FoodCode.ToString() == listCS.Any().ToString());
if (itmAlreadyExistsCnt == 1)
rtnMsg = "Detail corresponding to Item already exist.";
else  (itmAlreadyExistsCnt > 1)  //Multiple items exists 
rtnMsg = "Detail corresponding to some Item(s) already exist.";
}

尝试获取请求。输出

找到解决方案了。如下所示:

public void GetCSWSTUCPAFreeRestrictBlkFood(int FreeRestrictBlk, bool isCateg, string categoryId, string subCategoryId)
{
string rtnMsg = string.Empty;
int itmAlreadyExistsCnt = 0;
List<CurrentDietSummary> restrData = null;
restrData = GetPARestrictedFood();
var listCS = hfCheckedChildCS.Value.Split(',').ToList();
if (restrData != null && restrData.Count > 0)
itmAlreadyExistsCnt = restrData.Count(x => x.CategoryId == Convert.ToInt32(categoryId)
&& x.SubCategoryId == Convert.ToInt32(subCategoryId)
&& listCS.Contains(x.FoodCode.ToString()));
if (itmAlreadyExistsCnt == 1)
rtnMsg = "Detail corresponding to Item already exist.";
else  (itmAlreadyExistsCnt > 1)  //Multiple items exists 
rtnMsg = "Detail corresponding to some Item(s) already exist.";
}

相关内容

  • 没有找到相关文章

最新更新