Hi
I have below code & i want to store values in a string with comma separation. Last comma value also to be removed
C#
BALStudents bALStudents = new BALStudents();
List objStudentList = bALStudents.GetAvailableStudntList(Convert.ToInt32(colum.GroupID));
if (objStudentList != null)
{
}Thanks

Jignesh KumarRank #91 · 22.9K reputationPosted Mar 30, 2023, 1:41 AM EDT
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 29, 2023, 8:07 AM EDT
Hello Ramco,
Try below:
BALStudents bALStudents = new BALStudents(); List objStudentList = bALStudents.GetAvailableStudntList(Convert.ToInt32(colum.GroupID)); if (objStudentList != null) { string commaSeparatedValues = string.Join(",", objStudentList.Select(s => s.StudentName)); // The above line will concatenate all the StudentName values with comma separation. // You can replace "StudentName" with any property you want to include in the string. // If you want to exclude the last comma, you can use the following code instead: // string commaSeparatedValues = string.Join(",", objStudentList.Select(s => s.StudentName)).TrimEnd(','); }Thanks
Naimish Makwana
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 29, 2023, 8:31 AM EDT
Hello Ramco,
As Naimish told you can create comma seperated list using string.Join(",",ListOfValue),
string.Join(",",ListOfValue) takes two parameter
1. First Parameter Which is seperator you want to use in your case , (comma)
2. Second Parameter Which is list Object your want to club togather
Below code will gives you commaSeperated studentIds, Its depend on your requirement you can change field name.
BALStudents bALStudents = new BALStudents(); List objStudentList = bALStudents.GetAvailableStudntList(Convert.ToInt32(colum.GroupID)); if (objStudentList != null) { string commaSeparatedValues = string.Join(",", objStudentList.Select(s => s.StudentId).ToList()); }Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 29, 2023, 4:23 PM EDT
foreach (StudentDetail student in objStudentList) { string name = student.Name; // Do something with the name variable }In the given code, the
objStudentListvariable is a List ofStudentDetailobjects which are obtained by calling theGetAvailableStudntListmethod ofBALStudentsobject with a parameter ofcolum.GroupID.To extract values from the
objStudentListobject, you can use a loop like aforloop orforeachloop. Here's an example usingforeachloop to iterate over theobjStudentListand extract theNameproperty of eachStudentDetailobject:n this example, the
Nameproperty of eachStudentDetailobject is extracted and stored in thenamevariable. You can then use this variable for further processing.Ramco RamcoRank #462 · 5.4K reputationPosted Mar 30, 2023, 12:45 AM EDT
Hi
i have written below code but comma's not getting inserted between 2 names
Chilumuri - 9814604990Rana - 7738499995
foreach (var colum1 in objStudentList) { StudentDetail objResult = bALStudents.GetStudentDetailByLoginID(Convert.ToInt32(colum1.StudentID)); StudentName += string.Join(",", objResult.Name+objResult.MobileNo)+ "" ; }Jignesh KumarRank #91 · 22.9K reputationPosted Mar 30, 2023, 1:25 AM EDT
Hello Ramco,
Try this one,
BALStudents bALStudents = new BALStudents(); List objStudentList = bALStudents.GetAvailableStudntList(Convert.ToInt32(colum.GroupID)); if (objStudentList != null) { string studentNameCommaseperated = string.Join(",", objStudentList.Select(c => new {StudentName = string.Format("{0} - {1}", c.Name, c.MobileNo) }).ToList(); }Jignesh KumarRank #91 · 22.9K reputationPosted Mar 30, 2023, 1:31 AM EDT
Hello Ramco,
Please try this one,
BALStudents bALStudents = new BALStudents(); List objStudentList = bALStudents.GetAvailableStudntList(Convert.ToInt32(colum.GroupID)); string commaseperatedStudentName = ""; if (objStudentList != null) { commaseperatedStudentName = string.Join(",", objStudentList.Select(c => string.Format("{0} - {1}", c.Name, c.MobileNo)).ToList()); }Ramco RamcoRank #462 · 5.4K reputationPosted Mar 30, 2023, 1:37 AM EDT
Hi Jignesh
I have written like below . How i should remove last comma
StudentName += objResult.Name + "" + "," + " ";Thanks