简体   繁体   中英

c# linq SELECT list inside another list

Here My Classes

public class EmployeeProjectMaster
{
    public int EmployeeProjectMasterId { get; set; }
    //Class Variables    
    //Here i wants to get list of EmployeeProjectTransaction 
    //this is the way i tried
     public EmployeeProjectMasterModelClient()
    {
        this.EmployeeProjectTransactionModel = new HashSet<EmployeeProjectTransactionModelClient>();
    }
    public virtual IEnumerable<EmployeeProjectTransactionModelClient> EmployeeProjectTransactionModel { get; set; }    
}
public class EmployeeProjectTransaction
{
    public int EmployeeProjecTransactiontId { get; set; }
    public int EmployeeProjectMasterId { get; set; }
    public int EmployeeId { get; set; }
    public string EmployeeName {get;set;}
    //Class Variables        
}
public class Employee
{
    public int EmployeeId { get; set; }
    public string Employeefullname {get;set;}        
}

Then here my linq query

//Get All Employee Project Details To Grid
    public ActionResult GetAllEmployeeProjectDetails()
    {
        //DataTable Parameter
        var draw = Request.Form.GetValues("draw").FirstOrDefault();
        //Paging parameter
        var start = Request.Form.GetValues("start").FirstOrDefault();
        var length = Request.Form.GetValues("length").FirstOrDefault();
        //Paging parameter
        var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
        var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();

        //filter parameter
        var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();

        List<EmployeeProjectMasterModelClient> allEmployeeProject = new List<EmployeeProjectMasterModelClient>();
        int pageSize = length != null ? Convert.ToInt32(length) : 0;
        int skip = start != null ? Convert.ToInt32(start) : 0;
        int recordsTotal = 0;

        //Database Query
        using (InnoESolutionsDbEntities oInnoESolutionsDbEntities = new InnoESolutionsDbEntities())
        {
            var v = (from Epmm in oInnoESolutionsDbEntities.EmployeeProjectMasterModels
                join Eptm in oInnoESolutionsDbEntities.EmployeeProjectTransactionModels
                    on Epmm.EmployeeProjectMasterId equals Eptm.EmployeeProjectMasterId
                select new {Epmm, Eptm});

            if (!string.IsNullOrEmpty(searchValue))
            {
                v = v.Where(b =>
                b.Epmm.ProjectModel.ProjectName.Contains(searchValue)
                );
            }

            if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
            {
                //for make sort simpler we will add system.linq.dynamic reference
                //v = v.OrderBy(sortColumn + " " + sortColumnDir);
                v = v.OrderBy(a => a.Epmm.EmployeeProjectMasterId + " " + a.Eptm.EmployeeProjectMasterId);
            }
            recordsTotal = v.Count();
            allEmployeeProject = v.Skip(skip).Take(pageSize).Where(y => y.Epmm.IsActive && !y.Epmm.IsDelete && y.Eptm.IsActive && !y.Eptm.IsDelete).Select(x => new EmployeeProjectMasterModelClient
            {
                EmployeeProjectMasterId = x.Epmm.EmployeeProjectMasterId,
                ProjectId = x.Epmm.ProjectId,
                ProjectName = x.Epmm.ProjectModel.ProjectName,
                WorkDateS = SqlFunctions.DateName("day", x.Epmm.WorkDate) + "/ " + SqlFunctions.DateName("month", x.Epmm.WorkDate) + "/ " + SqlFunctions.DateName("year", x.Epmm.WorkDate),
                SalaryForEachEmployee = x.Epmm.SalaryForEachEmployee,
                EmployeeProjectTransactionModel = *************************************How to Load Trnsaction Details As A list Here

            }).ToList();
        }
        return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = allEmployeeProject });

    }

Above c# function and Linq query for load angular data table. it's needs to select List Inside a List. i Wrote linq query for join. I don't know whether If it is correct or not.when you answer my question please consider that query. Have tried lot of time but i have failed. in query when i try to get record into allEmployeeProject . how to load transaction Details As a list inside allEmployeeProject list asterisk symbol indicates what i need to do. i wants list of employee for particular master record.

Here output what i want

MasterId | TransactionId |Employee Id | Employee Name 

---------+---------------+------------+---------------
1        |       1       |      4     | name 1
         |       2       |      2     | name 3

I think what you need is GroupBy method.

allEmployeeProject = v.Skip(skip).Take(pageSize).Where(y => y.Epmm.IsActive && !y.Epmm.IsDelete && y.Eptm.IsActive && !y.Eptm.IsDelete).Select(x => new EmployeeProjectMasterModelClient
        {
            EmployeeProjectMasterId = x.Epmm.EmployeeProjectMasterId,
            ProjectId = x.Epmm.ProjectId,
            ProjectName = x.Epmm.ProjectModel.ProjectName,
            WorkDateS = SqlFunctions.DateName("day", x.Epmm.WorkDate) + "/ " + SqlFunctions.DateName("month", x.Epmm.WorkDate) + "/ " + SqlFunctions.DateName("year", x.Epmm.WorkDate),
            SalaryForEachEmployee = x.Epmm.SalaryForEachEmployee
        });

var allEmployeeProjectsByMaster = allEmployeeProject.GroupBy(x => x.EmployeeProjectMasterId).ToList();

Now you can have transaction list grouped by EmployeeProjectMaster with allEmployeeProjectsByMaster variable, just map it to relevant data model using Select .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM