API-实体框架核心2.1返回子信息

时间:2019-02-27 16:45:22

标签: entity-framework core

我已经编写了Core 2.1 API,我正尝试检索该测试的子测试类型信息。

这是父TestInfo模型类:

 public partial class TestInfo
{

    public int TestId { get; set; }
    public string TestShortDescription { get; set; }
    public string TestLongDescription { get; set; }
    public int TestTypeId { get; set; }
    public int? TestLimitsId { get; set; }
    public ICollection<TestTypeInfo> TestTypeInfos { get; set; }
}

这是子TestTypeInfo模型类:

public partial class TestTypeInfo
{
    [ForeignKey("TestInfo")]
    public int TestTypeId { get; set; }
    public string TestTypeShortDescription { get; set; }
    public string TestTypeLongDescription { get; set; }
    public TestInfo TestInfo { get; set; }
}

这是我的控制器代码。我正在做一个包含以引入测试类型信息。我的语法错误,可以使用一些帮助...

[HttpGet]
    public IEnumerable<TestInfo> GetTestInfo()
    {
        var testinfo = _context.TestInfo.Include(t => t.TestTypeInfos);
        return _context.TestInfo.Include(t => t.TestTypeInfos).ToList();
    }

问题在于返回的测试类型信息基于TestInfo TestID而不是TestInfo TestTypeID。请参见下图。有人可以告诉我如何根据测试的TestInfo TestTypeID返回测试类型信息。

enter image description here

2 个答案:

答案 0 :(得分:0)

根据您的其他信息更新了答案。

[HttpGet]
public IEnumerable<TestInfo> GetTestInfo()
{
    return _context.TestInfo
        .Select(c => new TestInfo()
        {
            TestId = c.TestId,
            TestShortDescription = c.TestShortDescription,
            TestLongDescription = c.TestLongDescription,
            TestTypeId = c.TestTypeId,
            TestLimitsId = c.TestLimitsId,
            TestTypeInfos = c.TestTypeInfos.Where(x => x.TestTypeId == c.TestTypeId).ToList()
        })
        .ToList();
}

答案 1 :(得分:0)

这是我要工作的显式编码示例:

  [HttpGet]
    public IEnumerable<TestInfo> GetTestInfo()
    {

       List<TestInfo> tstinfoIn = _context.TestInfo.ToList();
       List<TestTypeInfo> tsttypinfoIn = _context.TestTypeInfo.ToList();
       List<TestInfo> tstinfoComboOut = new List<TestInfo>();

       tstinfoComboOut = tstinfoIn
                       .Select(c => new TestInfo()
                       {
                           TestId = c.TestId,
                           TestShortDescription = c.TestShortDescription,
                           TestLongDescription = c.TestLongDescription,
                           TestTypeId = c.TestTypeId,
                           TestLimitsId = c.TestLimitsId,
                           TestTypeInfos = GetTestTypeInfo(tsttypinfoIn, c.TestTypeId)
                       })
                       .ToList();

        return tstinfoComboOut; 
    }

以下是用于检索子级的GetTestTypeInfo方法:

 public static List<TestTypeInfo> GetTestTypeInfo(List<TestTypeInfo> tsttypinfoIn, int parentTestTypeId)
    {
        return tsttypinfoIn
                .Where(c => c.TestTypeId == parentTestTypeId)
                .Select(c => new TestTypeInfo
                {
                    TestTypeId = c.TestTypeId,
                    TestTypeShortDescription= c.TestTypeShortDescription,
                    TestTypeLongDescription = c.TestTypeLongDescription
                })
                .ToList();
    }

请告诉我是否有更好的方法...