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

cx6n0qe3  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(200)

我创建了一个包含食品类别、子类别、代码和食品名称的列表(例如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.";
}

已尝试获取请求输出

3pmvbmvn

3pmvbmvn1#

得到了解决方案。像下面这样做:

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.";
}

相关问题