Home > Mobile >  LINQ to get unique id that has list of items
LINQ to get unique id that has list of items

Time:07-13

I am trying to generate a list result through linq. I would like to have a result as a list with unique folder id with 0th item as folder_name, 1st Item as a list with one or more projects having fields project_id,name and description. I have written following query: Folders is basically model with format as folderid, name, List projects where Project Model has project_id, name and description

from the following records:

Models

public class FolderModel
{
  public int folder_id { get; set; }
  public string folder_name { get; set; }
  public List<ProjectModel> projects{ get; set; }
}

public class ProjectModel
{
    public int project_id { get; set; }
    public string project_name { get; set; }
    public string project_description { get; set; }
}

public class ResultModel
{  
  public List<FolderModel> folders { get; set; } 
}

Result Data

List<FolderModel, List<ProjectModel>> result = new List<FolderModel, List<ProjectModel>>();

List<ProjectModel> projectList1 = new List<>();
ProjectModel projectModel1 = new ProjectModel();
projectModel1.project_name = "F1P1";
projectModel1.project_description = "F1P1";
projectList1.add(projectModel1);

List<ProjectModel> projectList2 = new List<>();
ProjectModel projectModel21 = new ProjectModel();
projectModel21.project_name = "F2P1";
projectModel21.project_description = "F2P1";
projectList2.add(projectModel21);

ProjectModel projectModel22 = new ProjectModel();
projectModel22.project_name = "F2P2";
projectModel22.project_description = "F2P2";
projectList2.add(projectModel22);

List<ProjectModel> projectList3 = new List<>();
ProjectModel projectModel3 = new ProjectModel();
projectModel3.project_name = "F3P1";
projectModel3.project_description = "F1P3";
projectList3.add(projectModel3);

ResultModel resultModel = new resultModel();
resultModel.(new FolderModel { folder_id: 1,folder-name: "F1" }, projectList1);

FolderModel folderModel1 = new FolderModel();
folderModel1.folder_id = 1
folderModel1.folder_name = "F1"
folderModel1.projects = projectList1

FolderModel folderModel2 = new FolderModel();
folderModel2.folder_id = 2
folderModel2.folder_name = "F2"
folderModel2.projects = projectList2

FolderModel folderModel3 = new FolderModel();
folderModel3.folder_id = 3
folderModel3.folder_name = "F3"
folderModel3.projects = projectList3

ResultModel resultModel = new ResultModel();
List<FolderModel> folderList = new List<>();
folderList.add(folderModel1);
folderList.add(folderModel2);
folderList.add(folderModel3);
resultModel.folders = folderList

SQL Data

folder_id | folder_name | project_id |  project_name  | project_description
1            F1               11         F1P1            F1P1
2            F2               21         F2P1            F2P1   
2            F2               22         F2P2            F2P2
3            F3               31         F3P1            F3P1
4            F4               41         F4P1            F4P1
5            F5               51         F5P1            F5P1   

This is what I have tried

var result = resultModel.folders.GroupBy(x => new { x.folder_id }).ToList();

I am sure that after group by I have to select name but not sure how can I generate project into a list and add it to main folder id. Can someone please guide on this.

CodePudding user response:

It seems to me that you're looking for this:

var folders = new []
{
    new { folder_id = 1, folder_name = "F1", project_id = 11, project_name = "F1P1", project_description = "F1P1" },
    new { folder_id = 2, folder_name = "F2", project_id = 21, project_name = "F2P1", project_description = "F2P1" },
    new { folder_id = 2, folder_name = "F2", project_id = 22, project_name = "F2P2", project_description = "F2P2" },
    new { folder_id = 3, folder_name = "F3", project_id = 31, project_name = "F3P1", project_description = "F3P1" },
    new { folder_id = 4, folder_name = "F4", project_id = 41, project_name = "F4P1", project_description = "F4P1" },
    new { folder_id = 5, folder_name = "F5", project_id = 51, project_name = "F5P1", project_description = "F5P1" },
};

List<FolderModel> result =
    folders
        .GroupBy(
            x => new { x.folder_id, x.folder_name },
            x => new { x.project_id, x.project_name, x.project_description })
        .Select(x => new FolderModel()
        {
            folder_id = x.Key.folder_id,
            folder_name = x.Key.folder_name,
            projects = x.Select(y => new ProjectModel()
            {
                project_id = y.project_id,
                project_name = y.project_name,
                project_description = y.project_description,
            }).ToList(),
        })
        .ToList();

CodePudding user response:

You can query like this:

var result =
    resultModel
        .folders
            .GroupBy(x => new { x.folder_id })
            .Select(s => new
            {
                folder_id = s.Key,
                folder_name = s.First().folder_name
            })
            .ToList();
  • Related