2014-12-24 25 views
-1

我曾經有一個DataTable,我會用這個查詢:C#自定義對象列表 - 找對象使用SQL LIKE語句

rows = dtpc.Select("POSTCODE LIKE '" + postcodeID + "%'"); 

這將返回許多行,我可以再從數據表中刪除。

現在我正在使用customerobjects列表而不是數據表。

[Serializable] 
    public class CUSTOMEROBJECT 
    { 
     public string Rep_code { get; set; } 

     public string Int_rep_hou { get; set; } 

     public string Int_rep_key { get; set; } 

     public string Fullname { get; set; } 

     public string Custcode { get; set; } 

     public string Category { get; set; } 

     public string Address1 { get; set; } 

     public string Address2 { get; set; } 

     public string Address3 { get; set; } 

     public string Postcode { get; set; } 

     public string Country { get; set; } 

     public string Telephone { get; set; } 

     public double Lat { get; set; } 

     public double Lng { get; set; } 

     public string County { get; set; } 
    } 

如何使用類似於上面使用的LIKE語句的方式找到客戶對象的選擇?

+0

'list.Where(W => w.Postcode.StartsWith(postcodeID)' –

回答

2

您可以使用LINQ

customerList.Where(c => c.Postcode.StartsWith(postcodeID.ToString())); 
+0

謝謝你,不過我將如何去了解這一點:?如果postcodeid是「BW '它會返回所有很好的BW郵政編碼,但是如果郵政編碼只是'B'或單個字符,我只想返回'B1'B2'B3'B4'等,而不是'BW'。 – user4316519

+0

使用substring提取郵政編碼的第一個字符,並在linq查詢中使用該字符代替郵政編碼 – failedprogramming

+0

嗨,是的,我可以得到第一個字符,但使用.Startwith將返回例如B1,​​B2,B3和BW BG BH等。我只想返回B1,B2,B3謝謝 – user4316519