Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq组运算符展平查询结果的正确方法是什么_C#_Linq_Nhibernate - Fatal编程技术网

C# 使用linq组运算符展平查询结果的正确方法是什么

C# 使用linq组运算符展平查询结果的正确方法是什么,c#,linq,nhibernate,C#,Linq,Nhibernate,这是数据结构,我从NHibernate查询中得到 public Class PaperResult { public Guid SubjectId {get;set} public Guid StudentId {get;set} public string Name {get;set} public string Email {get;set} public int Marks {get;set} } 我的问题是 var resultEnt

这是数据结构,我从NHibernate查询中得到

public Class PaperResult 
{
    public Guid SubjectId {get;set}

    public Guid StudentId {get;set}

    public string Name {get;set}

    public string Email {get;set}

    public int Marks {get;set}

}
我的问题是

  • var resultEntities=_repository.Query()。其中(t=>t.Id==testId)
  • studentResults=resultenties.GroupBy(x=>x.StudentId)
  • 返回,IQueryable,现在我将它投射到类中

    public Class StudentResult 
    {
        public Guid StudentId {get;set}
    
        public string Name {get;set}
    
        public string Email {get;set}
    
        public int Marks {get;set}
    
        public IEnumerable<PaperResult> PaperResults {get;set;}
    }
    
    注:该代码为骨架代码


    关于,

    您应该能够对一种特殊类型进行投影

    然后投影到所需的类型:
    q、 AsEnumerable()。选择(…)

    您有do resultEntities.toList()。在将对象映射到StudentResult之前。通常,在LINQ中展平数据结构的“正确”方法是通过SelectMany()运算符。您当然应该看看它,我不确定是否通过Nhibernate支持此功能。:)@HemantmalPorte对延迟结果执行ToList()操作将在内存中获取数据,我希望避免这种情况,因为它会导致性能问题。@DotNetHitMan any references pls???@SrinivasRa-是一篇好文章。如果你需要一个有数据的例子,我可以给你举一个。
    from paperResult in studentResults
    let studentResult  = paperResult.First()
    select new StudentResult()
    {
        StudentId =studentResult.StudentId,
        Name =studentResult.Name,
        Email =studentResult.Email,
        Marks =studentResult.Marks,
        PaperResults =resultEntities.Where(x => x.StudentId ==studentResult.StudentId
                      select new PaperResult(){...}.ToList()
    }