代码之家  ›  专栏  ›  技术社区  ›  David

foreach的where和condition参数必须由两个变量检查

  •  -1
  • David  · 技术社区  · 7 年前

    innerJoinQuery.Where(item => 
        (string.IsNullOrWhiteSpace(Dname) || item.StudentName== Dname) 
        && 
        (string.IsNullOrWhiteSpace(Gname) || item.StudentName== Gname)
    )
    

    条件是如果我不传任何名字,我就会得到全部学生名单。

    如果我只通过了Dname,我会得到以D开头的学生名单。

    如果我只通过了Gname,我会得到以G开头的学生名单。 它起作用了

    更新

    如果我使用此代码:

            (string.IsNullOrWhiteSpace(Dname) && string.IsNullOrWhiteSpace(Gname ) 
        || item.StudentName.ToString() == Dname.ToString() ||
    item.StudentName.ToString() == Gname.ToString())
    

    只发送Dname,我得到一个例外:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    
    Gname was null.
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Rafalon    7 年前

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Student
    {
        public int Id {get;set;}
        public string Name {get;set;}
    }
    
    class Program
    {
        static List<Student> Students {get;set;}
    
        static void Main()
        {
            Students = new List<Student>
            {
                new Student{ Id = 1, Name = "G"},
                new Student{ Id = 2, Name = "D"},
                new Student{ Id = 3, Name = "G"},
                new Student{ Id = 4, Name = "G"},
                new Student{ Id = 5, Name = "D"},
                new Student{ Id = 6, Name = "E"},
                new Student{ Id = 7, Name = "F"},
                new Student{ Id = 8, Name = "G"},
                new Student{ Id = 9, Name = "H"}
            };
    
            WriteStudents(null, null); // 1,2,3,4,5,6,7,8,9
            WriteStudents(null, "D");  // 2,5
            WriteStudents("G", null);  // 1,3,4,8
            WriteStudents("G", "D");   // 1,2,3,4,5,8
        }
    
        static void WriteStudents(string GName, string DName)
        {
            var query = Students.Where(s => string.IsNullOrWhiteSpace(GName)
                                         && string.IsNullOrWhiteSpace(DName)
                                         || s.Name == GName || s.Name == DName)
                                .Select(s => s.Id); // used to print the Id
    
            Console.WriteLine(string.Join(",", query));
        }
    }
    

    显然 WriteStudents 方法可以写为:

    static void WriteStudents(string first = null, string second = null)
    {
        var query = Students.Where(s => string.IsNullOrWhiteSpace(first)
                                     && string.IsNullOrWhiteSpace(second)
                                     || s.Name == first || s.Name == second)
                            .Select(s => s.Id); // used to print the Id
    
        Console.WriteLine(string.Join(",", query));
    }
    

    所以你可以称之为:

    WriteStudents();         // 1,2,3,4,5,6,7,8,9
    WriteStudents("D");      // 2,5
    WriteStudents("G");      // 1,3,4,8
    WriteStudents("G", "D"); // 1,2,3,4,5,8