C# 和 Linq:Where 子句包含一些值并排除其他值

匿名
2024-03-28T07:31:18.9+00:00

使用 .net Framework 4.8,我试图编写一些 Linq,其中 Where 子句包含满足某些条件的项目 - 但排除满足其他一些条件的这些项目的子集。以下是情况的摘要,我将在下面提供完整的示例代码:

我有一个对象列表,其中 2 个属性是: ParmDir(字符串:即 IN、OUT、INOUT) ParmName(字符串:即 @Id、@Parm1、@ErrorLogId、@OutParm1、@OutParm2等......

1)我想过滤ParmDircontians“IN”或“INOUT”的列表。 2)其中,我想排除ParmName包含“@ErrorLogId”的任何项目。

我有第一步工作:

var including = new string[] { "OUT", "INOUT" };  
outPutCount = list.Where(x => including.Contains(x.ParmDir)).Count();  

但是排除类似内容的语法是什么:

var excluding = new string[] { "ErrorLogId" };  
x <> excluding.Contains(x.ParmName)  

所以完整的语句是这样的:

var including = new string[] { "OUT", "INOUT" };  
var excluding = new string[] { "ErrorLogId" };  
outPutCount = list.Where(x => including.Contains(x.ParmDir) && x <> excluding.Contains(x.ParmName)).Count();  

谢谢。

下面是要在控制台应用中测试的所有代码:

class Program  
{  
    static void Main(string[] args)  
    {  
        List<Parm> list = new List<Parm>();  
        list.Add(new Parm("@Id", "IN", 1, "uniqueidentifier"));  
        list.Add(new Parm("@Parm1", "IN", 2, "varchar"));  
        list.Add(new Parm("@Parm2", "IN", 3, "int"));  
        list.Add(new Parm("@Parm3", "IN", 4, "decimal"));  
        list.Add(new Parm("@ErrorLogId", "INOUT", 4, "uniqueidentifier"));  
        list.Add(new Parm("@OutParm1", "OUT", 5, "int"));  
        list.Add(new Parm("@OutParm2", "INOUT", 6, "varchar"));  
        list.Add(new Parm("@OutParm3", "INOUT", 7, "decimal"));  
        list.Add(new Parm("@OutParm4", "INOUT", 8, "int"));  

        int totalCount = 0;  
        int outPutCount = 0;  
        int errorLogCount = 0;  

        totalCount = (from x in list select x).Count();  

        var including = new string[] { "OUT", "INOUT" };  
        var excluding = new string[] { "ErrorLogId" };  
        outPutCount = list.Where(x => including.Contains(x.ParmDir)).Count();  

        Console.WriteLine("totalCount = " + totalCount.ToString());  
        Console.WriteLine("outPutCount = " + outPutCount.ToString());  
    }  

    public class Parm  
    {  
        string _parmName;  
        string _parmDir;  
        int _sort;  
        string _sQLDataType;  

        public Parm(string parmName, string parmDir, int sort, string sQLDataType)  
        {  
            _parmName = parmName;  
            _parmDir = parmDir;  
            _sort = sort;  
            _sQLDataType = sQLDataType;  
        }  
        public string ParmName { get => _parmName; set => _parmName = value; }  
        public string ParmDir { get => _parmDir; set => _parmDir = value; }  
        public int Sort { get => _sort; set => _sort = value; }  
        public string SQLDataType { get => _sQLDataType; set => _sQLDataType = value; }  
    }  
}  

Note:此问题总结整理于: C# and Linq: Where clause contains some value and exludes other values

开发人员技术 C#
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 48,676 信誉分 Microsoft 外部员工
    2024-03-28T09:03:59.5766667+00:00

    尝试使用来 != 确定字符串是否相等:

                var re = list.Where(x => including.Contains(x.ParmDir) && x.ParmName != "@ErrorLogId");  
    

            var excluding = new string[] { "@ErrorLogId" };  
            var re = list.Where(x => including.Contains(x.ParmDir) && !excluding.Contains(x.ParmName));  
    

    如果回复有帮助,请点击“接受答案”并点赞。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。