使用LINQ LAMBDA基于common id加入两个类

时间:2015-11-03 11:02:22

标签: c# linq list lambda

我想基于一个公共id连接到两个类,它们使用lambda / linq从父表生成一行,从子表生成两行。我已粘贴到下面,我到目前为止尝试了但是我得到的是每行10行,这是不正确的。欢迎对代码提出任何建议或修改。

namespace x
{
    public class IntegrationSearchResult
    {
        public string Dfe { get; set; }
        public SchoolResult SchoolSearchResult { get; set; }
        public List<PupilResult> PupilSearchResult { get; set; }
    }

    public class SchoolResult
    {
        public int SupplierId { get; set; }
        public string SchoolName { get; set; }              
        public DateTime CreateDate { get; set; }
        public int Pupils { get; set; }     
    }

    public class PupilResult
    {
        public int SupplierId { get; set; }     
        public string FirstName { get; set; }
        public string LastName { get; set; }        
    }
}

功能

private Func<IEnumerable<IntegrationSearchResult>> _getAllDummySearchResults;
private Func<string, IntegrationSearchResult> _getDfeDummySearchSchool;

我尝试获得结果

_

getAllDummySearchResults = () => (from dummySupplierId in Enumerable.Range(1, 10)                                                      
    select new IntegrationSearchResult
    {
        Dfe = "School-1-" + dummySupplierId.ToString("000000"),

        SchoolSearchResult = new SchoolResult
        {
            SupplierId = dummySupplierId,
            SchoolName = "SchoolName" + dummySupplierId.ToString("000000"),         
            CreateDate = new DateTime(),
            Pupils = 123            
        }
        ,
        PupilSearchResult = new List<PupilResult>
        {
            new PupilResult
            {
                SupplierId = dummySupplierId,                    
                FirstName = "FirstName - Pupil-1-" + dummySupplierId.ToString("000000"),
                LastName = "LastName - Pupil-1-" + dummySupplierId.ToString("000000")
            },
            new PupilResult
            {
                SupplierId = dummySupplierId,
                FirstName = "FirstName - Pupil-2-" + dummySupplierId.ToString("000000"),
                LastName = "LastName - Pupil-2-" + dummySupplierId.ToString("000000")
            }
        }
    }).ToList();

    _getDfeDummySearchSchool = dfe =>
    {
        var allSchools = _getAllDummySearchResults();
        return allSchools.FirstOrDefault(x => x.Dfe == dfe);
    };

预期结果:学校SearchResult应显示一行,并且相应的学生是两行,具有相同的供应商ID。

{
  "Dfe": 0,
  "SchoolSearchResult": [
    {
      "SupplierId": 1,
      "SchoolName": "SchoolName000001",
      "CreateDate": "0001-01-01T00:00:00",
      "Pupils": 123
    }],
     "PupilSearchResult": [
    {
      "SupplierId": 1,
      "FirstName": "FirstName - Pupil-1-000001",
      "LastName": "LastName - Pupil-1-000001"
    },
    {
      "SupplierId": 1,
      "FirstName": "FirstName - Pupil-1-000002",
      "LastName": "LastName - Pupil-1-000002"
    }]
    }

1 个答案:

答案 0 :(得分:0)

您可以将第一种方法的定义更改为以下

getAllDummySearchResults = () => (from dummySupplierId in Enumerable.Range(1, 10)                                                      
    select new IntegrationSearchResult
    {
        Dfe = "School-1-" + dummySupplierId.ToString("000000"),

        SchoolSearchResult = new SchoolResult
        {
            SupplierId = dummySupplierId,
            SchoolName = "SchoolName" + dummySupplierId.ToString("000000"),         
            CreateDate = new DateTime(),
            Pupils = 123            
        }
        ,
        PupilSearchResult = new List<PupilResult>
        {
            new PupilResult
            {
                SupplierId = dummySupplierId,                    
                FirstName = "FirstName - Pupil-1-" + dummySupplierId.ToString("000000"),
                LastName = "LastName - Pupil-1-" + dummySupplierId.ToString("000000")
            },
            new PupilResult
            {
                SupplierId = dummySupplierId,
                FirstName = "FirstName - Pupil-2-" + dummySupplierId.ToString("000000"),
                LastName = "LastName - Pupil-2-" + dummySupplierId.ToString("000000")
            }
        }
    }).Take(1).ToList();

它只从子集合中选择两个记录,当你在第二个方法上调用相等时,它将调用第一个方法从子类中获取两行。