2016-11-18 72 views
2

我寫了下面的地址格式使用C#代碼

  string Address1 = ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString(); 
      string Address2 = ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString(); 
      string Address1C; 
      string Address2C; 

      if (Address1 != "") 
       Address1C = Address1 + ", "; 
      else 
       Address1C = Address1; 

      if (Address2 != "") 
       Address2C = Address2 + ", "; 
      else 
       Address2C = Address2; 



      lblAdderssX1.Text = Address1C + Address2C; 

      string City = ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString(); 
      string CityC; 
      if (City != "") 
       CityC = City + ", "; 
      else 
       CityC = City; 

      string Pin = ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString(); 

      string State = ds.Tables[0].Rows[0]["JurisdictionX"].ToString(); 
      string StateC; 

      if (State != "") 
       StateC = State + ", "; 
      else 
       StateC = State; 

      // string CountryC = ds.Tables[0].Rows[0]["CountryX"].ToString(); 

      lblAddressX2.Text = CityC + StateC + Pin; 

     "<asp:Label ID="lblAdderssX1" CssClass="ProfileLabel" runat="server"> </asp:Label> 
                        <asp:Label ID="lblAddressX2" CssClass="ProfileLabel" runat="server"> </asp:Label>" 

其實我們想要的格式應該像

「物理地址1,物理地址2,市格式化代碼行詳細地址,國家,引腳」

如果其中任何缺失讓我們說物理地址2然後地址應該是

‘物理地址1,市,州,銷’

,如果城市缺少在表中,那麼它應該像

「物理地址1,物理地址2,國家,引腳」

,如果物理地址2,城市,州,引腳缺失,則地址應該像

「物理地址1」和目前上面的是在這種情況下放置。我無法處理這個問題。請幫忙 !!!

而且如果沒有可用,則該文本應該來像「不可用」

回答

2

要更乾淨寫我想。創建List<string>

List<string> address = new List<string>(); 
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString()); 
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString()); 
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString()); 
address.Add(ds.Tables[0].Rows[0]["JurisdictionX"].ToString()); 
address.Add(ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString()); 

string list = string.Join(", ", address.Where(x => !string.IsNullOrEmpty(x))); 
+0

感謝偉大的答案,但它是在list.toArray() – Nida

+0

@Nida拋出錯誤,請您使用System.Linq的; ? – mybirthname

+0

是的,它已經在那裏它只在這條線上投擲 – Nida

1

串地址1 = ds.Tables [0] .Rows [0] [ 「PhysicalAddressLine1」]的ToString();

 string Address2 = ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString(); 

     string City = ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString(); 
     string State = ds.Tables[0].Rows[0]["JurisdictionX"].ToString(); 

     string Pin = ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString(); 

     string Address1C; 

     if(!string.IsNullOrEmpty(Address1)) 
      Address1C=Address1; 

     if (!string.IsNullOrEmpty(Address2)) 
     { 
      if (!string.IsNullOrEmpty(Address1C)) 
       Address1C = ", "+ Address2 ; 
      else 
      Address1C=Address2; 
     } 
     if (!string.IsNullOrEmpty(City)) 
     { 
      if (!string.IsNullOrEmpty(Address1C)) 
       Address1C = ", "+ City ; 
      else 
      Address1C=City; 
     } 

     if (!string.IsNullOrEmpty(State)) 
     { 
      if (!string.IsNullOrEmpty(Address1C)) 
       Address1C = ", "+ State ; 
      else 
      Address1C=State; 
     } 
    if (!string.IsNullOrEmpty(Pin)) 
     { 
      if (!string.IsNullOrEmpty(Address1C)) 
       Address1C = ", "+ Pin ; 
      else 
      Address1C=Pin; 
     } 
     if(!string.IsNullOrEmpty(Address1C)) 
      lblAdderssX1.Text = Address1C; 
     else 
     lblAdderssX1.Text = "Not Available";    


    //<asp:Label ID="lblAdderssX1" CssClass="ProfileLabel" runat="server"> </asp:Label> 
1

試試這個,

string address = string.Empty; 

if ((ds.Tables[0].Rows[0]["PhysicalAddressLine1"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString())) 
    address += ds.Tables[0].Rows[0]["PhysicalAddressLine1"].ToString() + ", "; 

if ((ds.Tables[0].Rows[0]["PhysicalAddressLine2"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString())) 
    address += ds.Tables[0].Rows[0]["PhysicalAddressLine2"].ToString() + ", "; 

if ((ds.Tables[0].Rows[0]["PhysicalAddressCity"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString())) 
    address += ds.Tables[0].Rows[0]["PhysicalAddressCity"].ToString() + ", "; 

if ((ds.Tables[0].Rows[0]["JurisdictionX"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["JurisdictionX"].ToString())) 
    address += ds.Tables[0].Rows[0]["JurisdictionX"].ToString() + ", "; 

if ((ds.Tables[0].Rows[0]["PhysicalAddressPin"] != null) && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString())) 
    address += ds.Tables[0].Rows[0]["PhysicalAddressPin"].ToString() + ", "; 

address = address.TrimEnd(", ");