Tôi có một mảng các đối tượng chứa chuỗi.Tham gia các chuỗi duy nhất từ một mảng đối tượng
var values = new object[5];
values[0] = "PIZZA HUT";
values[1] = "ISLAMABAD";
values[2] = "ISLAMABAD";
values[3] = "PAKISTAN";
values[4] = "PAKISTAN";
Tôi muốn có được một chuỗi các yếu tố độc đáo từ mảng, tham gia với ,
cũng cần phải kiểm tra xem chuỗi isNullOrWhiteSpace;
PIZZA HUT, ISLAMABAD, PAKISTAN.
Hiện tại tôi đang làm như sau. Nhưng bạn có thể thấy rằng nó cần thiết để kiểm tra nhiều trong câu lệnh if. Tôi đã tự hỏi nếu có một cách tốt hơn sử dụng LINQ
string featureName = values[0] as string;
string adminboundry4 = values[1] as string;
string adminboundry3 = values[2] as string;
string adminboundry2 = values[3] as string;
string adminboundry1 = values[4] as string;
if (!string.IsNullOrWhiteSpace(adminboundry4)
&& adminboundry4 != adminboundry1
&& adminboundry4 != adminboundry2
&& adminboundry4 != adminboundry3) //want to get rid of these checks
featureName += "," + adminboundry4;
if (!string.IsNullOrWhiteSpace(adminboundry3)) //Not checking for duplicate here just for this question
featureName += "," + adminboundry3;
if (!string.IsNullOrWhiteSpace(adminboundry2)) //Not checking for duplicate here just for this question
featureName += "," + adminboundry2;
if (!string.IsNullOrWhiteSpace(adminboundry1)) //Not checking for duplicate here just for this question
featureName += "," + adminboundry1;
featureName
chứa PIZZA HUT, ISLAMABAD, PAKISTAN, PAKISTAN
thats những gì tôi muốn giảm thiểu –