简体   繁体   中英

Linq return type conversion

I have a linq query as follows,

    var result =
                    from Record in DBContext.GetAll().
                    group new { Record.Filed1, Record.Field2} by Record.Field3
                        into newGroup
                        select new
                        {
                            BodyRegion = newGroup.Key,
                            ByScanner =
                                from exam in newGroup
                                group exam.MaxValue by exam.Model
                                    into myGroup
                                    select myGroup,

                            ByExam =
                                from exam in newGroup
                                group exam.MaxValue by exam.Protocol
                                    into myGroup2
                                    select myGroup2
};

Then I iterate throught them,

 foreach (var rec in result)
            { 
                foreach (var byScanner in rec.ByScanner)
                  {
                     ProcessResult(byScanner.Key, byScanner.ToList());
                  }
                foreach (var byExam in rec.ByExam )
                  {
                     ProcessResult(byExam.Key, byExam.ToList());
                  }
            }

Everything works fine. But Iwant to move Linq query (first code snippet) to a function, what should be the return type the function? Return type of a function can not be var . If I give IEnumerable< Object > then while iterating I can't access rec.ByScanner, rec.ByExam because Object doesn't contain them. How to resolve this issue?

EDIT: I tried by creating a new class and filling them in that. But Grouping attributes byScanner. Key , byScanner. ToList() are not accessible. How this can be solved?

You are using an Anonymous Type . These shouldn't be passed around methods.

One thing you can do is create (Let's call it 'Record') a class with properties BodyRegion , ByScanner and ByExam and pass IEnumerable<Record> .

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