user6807402 user6807402 - 1 year ago 86
C# Question

In string List compare with same name

I have make one list str.

List<string> str = new List<string>();

str.AddRange(new string[] { "ahmedabad", "surat", "vadodara", "rajkot", "bhavnagar", "jamnagar", "gandhidham", "gandhinagar" });

I have create one class

class MatchAddress
public string Name { get; set; }

public string Taluka { get; set; }

public string District { get; set; }

public string Pincode { get; set; }

public float Rank { get; set; }

List<MatchAddress> lm;
List<MatchAddress> lmmatch.

fill the lm list.

lm.Add(new MatchAddress() { District = "vadodara", Taluka = "vadodara", Rank = 1f, Name = "vadodara" });

lm.Add(new MatchAddress() { District = "gandhinagar", Taluka = "gandhinagar", Rank = 1f, Name = "vadodara" });

foreach (MatchAddress ma in lm)
string pincode ="";
// what logic i write in below then only we got the vadodara because that name are in both taluka and name then only set nulll if any one differ then not set null.
if (str.Contains(ma.Name) && str.Contains(ma.Taluka.ToLower()))

pincode = null;


lmmatch.Add(new MatchAddress() { Name = ma.Name, District = ma.District, Pincode = (pincode == null)? null : ma.Pincode , Rank = rank, Taluka = ma.Taluka });

so in this lmmatch list for both the MatchAddress object set pincode null but i wan't null for vadodara only and that have same taluka and village name.
if the taluka and name are different then not set pincode null.

reason behind this is where in lm list where pincode and taluka both have vadodara then only set the pincode null .
but this code that i have write also set pincode null for name vadodara where taluka not same they have gandhinagar.

so after rus this code i have got two object and they both have set pincode null but i wan't to only for vadodara where they have both name and taluka same.

so any idea how i can solve this problem ?

Answer Source

You Need to change only your if you not need to ToLower also

if (str.Contains(ma.Name) && ma.Name.Equals(ma.Taluka))

You Just Need to comare ma.Name = ma.taluka simple

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download