简体   繁体   English

另一个列表内的C#linq SELECT列表

[英]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 然后在这里我的LINQ查询

//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. 上面的c#函数和Linq查询负载角数据表。 it's needs to select List Inside a List. 需要选择“列表内的列表”。 i Wrote linq query for join. 我写了linq查询加入。 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 . 当我尝试获取记录到allEmployeeProject的查询中。 how to load transaction Details As a list inside allEmployeeProject list asterisk symbol indicates what i need to do. 如何加载事务详细信息作为allEmployeeProject列表中的列表, 星号表示我需要做什么。 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. 我认为您需要的是GroupBy方法。

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 . 现在,您可以将交易清单按EmployeeProjectMaster与allEmployeeProjectsByMaster变量分组,只需使用Select将其映射到相关数据模型即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Javascript中访问C#List - Access C# List inside Javascript Javascript 等效于 C# LINQ Select - Javascript Equivalent to C# LINQ Select 选择靠近另一个的列表元素 - Select list elements close to another 在反应中迭代另一个列表中的列表 - iterate a list inside another in react 如何从内部的数据库表列表中传输选定的表 <select></select> 形成另一种形式 <select></select> 形成 - How can I transfer the selected table from a list of database tables inside <select></select> form to another <select></select> form 我有一个 Aps 列表,我需要 select asp.net 和 c# 中该列表的一个或多个元素 - I have a a list of Aps and i need to select one or multiple elements of that list in asp.net with c# 根据另一个列表确定列表的索引,反之亦然,欢迎使用C#/ VB.NET或JavaScript中的解决方案 - Determining the index of a list based on another list and vice versa, solutions in C#/VB.NET or JavaScript are welcome jQuery根据另一个选择列表填充选择列表 - JQuery to populate select list based on another select list 用另一个选择列表中的选项填充选择列表的最快方法 - Quickest way to populate a select list with options from another select list 根据另一个选择列表值选择选择列表值 - Selecting select list values depending on another select list value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM