.NET 4.0 এ, স্ট্রিং জোনে এর জন্য একটি ওভারলোড থাকে params object[]
তাই এটি এতটা সহজ:
int[] ids = new int[] { 1, 2, 3 };
string.Join(",", ids);
উদাহরণ
int[] ids = new int[] { 1, 2, 3 };
System.Data.Common.DbCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT * FROM some_table WHERE id_column IN (@bla)");
cmd.CommandText = cmd.CommandText.Replace("@bla", string.Join(",", ids));
.NET 2.0 এ, এটি একটি সামান্য সামান্য আরও কঠিন, যেহেতু এ জাতীয় কোনও ওভারলোড নেই। সুতরাং আপনার নিজের জেনেরিক পদ্ধতিটি প্রয়োজন:
public static string JoinArray<T>(string separator, T[] inputTypeArray)
{
string strRetValue = null;
System.Collections.Generic.List<string> ls = new System.Collections.Generic.List<string>();
for (int i = 0; i < inputTypeArray.Length; ++i)
{
string str = System.Convert.ToString(inputTypeArray[i], System.Globalization.CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(str))
{
// SQL-Escape
// if (typeof(T) == typeof(string))
// str = str.Replace("'", "''");
ls.Add(str);
} // End if (!string.IsNullOrEmpty(str))
} // Next i
strRetValue= string.Join(separator, ls.ToArray());
ls.Clear();
ls = null;
return strRetValue;
}
.NET 3.5 এ, আপনি এক্সটেনশন পদ্ধতিগুলি ব্যবহার করতে পারেন:
public static class ArrayEx
{
public static string JoinArray<T>(this T[] inputTypeArray, string separator)
{
string strRetValue = null;
System.Collections.Generic.List<string> ls = new System.Collections.Generic.List<string>();
for (int i = 0; i < inputTypeArray.Length; ++i)
{
string str = System.Convert.ToString(inputTypeArray[i], System.Globalization.CultureInfo.InvariantCulture);
if (!string.IsNullOrEmpty(str))
{
// SQL-Escape
// if (typeof(T) == typeof(string))
// str = str.Replace("'", "''");
ls.Add(str);
} // End if (!string.IsNullOrEmpty(str))
} // Next i
strRetValue= string.Join(separator, ls.ToArray());
ls.Clear();
ls = null;
return strRetValue;
}
}
সুতরাং আপনি joinArray এক্সটেনশন-পদ্ধতিটি ব্যবহার করতে পারেন।
int[] ids = new int[] { 1, 2, 3 };
string strIdList = ids.JoinArray(",");
আপনি যদি নিজের কোডটিতে এক্সটেনশনঅ্যাট্রিবিউট যোগ করেন তবে আপনি .NET 2.0 এ সেই এক্সটেনশন পদ্ধতিটিও ব্যবহার করতে পারেন:
// you need this once (only), and it must be in this namespace
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute {}
}