Hi
I have below code . In GroupCollection value is Group 2A , Group 2A(i) . I want to list all those records from Database which have Group value Group 2A , Group 2A(i) . I am tryng like below but not working
C#
public List GetMentorList(string Group)
{
List Result = (from t in context.Groups
where t.IsActive == true
&& (Group.Contains(t.GroupID.ToString()) : 1 == 1)
select t).ToList();
return Result;
}Thanks

Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 13, 2023, 9:36 AM EDT
public List GetMentorList(string Group) { List Result = (from t in context.Groups where t.IsActive == true && Group.Contains(t.GroupID.ToString()) select t).ToList(); return Result; }Vishal JoshiRank #377 · 6.7K reputationPosted Jun 15, 2023, 1:05 AM EDT
Hello,
You need to split the string and convert into list or array to check contains. Please try the below code.
public List GetMentorList(string Group) { List Result = new List(); if (!string.IsNullOrEmpty(Group)) { var groups = Group.Split(","); groups.ToList().ForEach(a => { a.Trim(); }); Result = (from t in context.Groups where t.IsActive == true && groups.Contains(t.GroupID.Trim().ToString()) select t).ToList(); } return Result; }Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 15, 2023, 10:42 PM EDT
public List GetMentorList(string groupId) { List result = context.Groups .Where(t => t.IsActive && groupId.Contains(t.GroupID.ToString())) .ToList(); return result; }I have made the following changes :
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Jun 16, 2023, 6:09 AM EDT
To retrieve records from the database based on a list of group values in C#, you can modify your code as follows:
public List GetMentorList(List groups) result = context.Groups
{
List
.Where(t => t.IsActive && groups.Contains(t.GroupID.ToString()))
.ToList();
return result;
}
In the code above, the
GetMentorListmethod takes a list of strings (groups) as a parameter, which represents the group values you want to search for. The LINQ query uses theWhereclause to filter the records based on the following conditions:t.IsActive: Ensures that only active records are considered.groups.Contains(t.GroupID.ToString()): Checks if theGroupIDconverted to a string exists in thegroupslist.Finally, the query is executed by calling
.ToList()to retrieve the filtered records as a list ofGroupobjects.Make sure to pass a valid list of group values to the
GetMentorListmethod when calling it.