Comma separated value from repeater on the base of selected checkbox

Hello All,

Sometimes we need to select comma separated IDs of all selected items from repeater.
In this situation we are using foreach loop for finding the selected checkbox items like below

string IDs = "";
foreach (RepeaterItem item in rptControl.Items)
	CheckBox chkID = (CheckBox)item.FindControl("chkID");
	if (chkID.Checked)
		HiddenField hdControlID = (HiddenField)item.FindControl("hdControlID");
		IDs = IDs + hdControlID.Value + ",";
IDs = (!string.IsNullOrEmpty(IDs) ? IDs.Substring(0, IDs.Length - 1) : IDs);

we can also do same thing using LINQ by using below code

 string IDs = string.Join(",", (from RepeaterItem item in rptControl.Items
				where (item.FindControl("chkID") as CheckBox).Checked
				select (item.FindControl("hdControlID") as HiddenField).Value).ToArray());

Convert Linq result to DataTable

Hello All,

Once I am stuck in LINQ query result, I need result as datatable but I didn’t find any linq method which directly converts result into datatable. So I have made one function which is used for converting linq result into datatable.

Please see the below method which is used for converting linq result set to datatable.

private DataTable AsDataTable<T>(IEnumerable<T> enumberable, string dtName)
   DataTable table = new DataTable(dtName);

   T first = enumberable.FirstOrDefault();
   if (first == null)
   return table;

   PropertyInfo[] properties = first.GetType().GetProperties();
   foreach (PropertyInfo pi in properties)
   table.Columns.Add(pi.Name, pi.PropertyType);

   foreach (T t in enumberable)
      DataRow row = table.NewRow();
      foreach (PropertyInfo pi in properties)
      row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, t, null);
   return table;

Now see the below code which is used above function to convert linq result to datatable.

IList<Object> data = new List<Object>();
data.Add(new { Name="Nikunj", ID="1", isFuturePay=true});
data.Add(new { Name = "Amit", ID = "2", isFuturePay = false });
data.Add(new { Name = "Raj", ID = "3", isFuturePay = true });
data.Add(new { Name = "Jaimin", ID = "4", isFuturePay = false });

DataTable table = AsDataTable((from d in data
select d), "CancelledError");