My Question . I have a table called category, this table had this column { ID, ParentId, name }. The ParentId is the same Id, as an example for this table:
Id | ParentId | Name |
---|---|---|
1 | NULL | Mobile |
2 | 1 | IPhone |
3 | 1 | Android |
4 | 2 | Accessorice |
Like that, I need to return data using generic repository pattern in.net core ape like:
ID | NAme | ParentName |
---|---|---|
2 | Iphone | Mobile |
3 | Iphone | Mobile |
4 | Accessories | iPhone |
Using generic repository you are limited in doing joins, unless you change the implementation of your code in generic repository class.
My advice is to get all of data initially in a variable and then manipulate it to get your required result, something like this:
var data = categoryRepository.GetAll();
List<CategoryVM> result = new List<CategoryVM>();
foreach(var item in data)
{
if(item.ParentId != null)
{
var parent = data.Where(x=>x.Id == item.ParentId).First();
result.Add(new CategoryVM()
{
Name = item.Name,
Id = item.Id,
ParentName = parent.Name
};
}
}
return Result;
Remember to have a viewmodel with the name of CategoryVM where you have those properties that are in your final resultant list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.