LINQ string[] against multiple fields

后端 未结 6 510
天命终不由人
天命终不由人 2021-01-02 20:59

Lets say I have a table dataContext.Customer with the following fields

    FName    varchar
    LName    varchar
    Phone    varchar
    DOB      datetime
          


        
6条回答
  •  星月不相逢
    2021-01-02 21:12

    Similar to Francisco's answer but with a single application of the where clause:

    string[] search = new string[] { "Bush", "111", "John" };
    var customers = new[]   { 
                                new {FName = "Dick", Surname = "Bush", Phone = "9512221111", DOB = new DateTime(1980,01,01), Address = "John Street 1" },
                                new {FName = "John", Surname = "Smith", Phone = "3051112222",  DOB =  new DateTime(1978,01,01), Address = "Roosevelt Av 787"}
                            };
    
    
    var result = customers.Where(customer => search.All(term =>
                        customer.FName.Contains(term)
                        || customer.Surname.Contains(term)
                        || customer.DOB.ToString().Contains(term)
                        || customer.Phone.Contains(term)
                        || customer.Address.Contains(term)
                        ));
    

提交回复
热议问题