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
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 SwainPosted Jun 13, 2023, 1:36 PM
Brahma Prakash ShuklaPosted Jun 16, 2023, 10:09 AM
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.Tuhin PaulPosted Jun 16, 2023, 2:42 AM
I have made the following changes :
Vishal JoshiPosted Jun 15, 2023, 5:05 AM
Hello,
You need to split the string and convert into list or array to check contains. Please try the below code.
Thanks