我有一个查询,该查询是对诸如TableATableBTableCTableD的一些表的联接,最后,我说的是select tableA与它返回的tableA roecrds一起使用。
然后,稍后我又有一个查询,它的tables, joins and where clause与第一个查询完全相同,但是唯一的区别是,最后我说的是select tableB

所以我的问题是我可以将它们组合在一起,这样就不必两次运行查询了吗?

        var query1 = (from ta in this.Context.TableA
                          join tb in this.Context.TableB on ta.Id equals tb.Id
                          join tc in blah
                          join td in blah
                          join te in blah
                          join tf in blah
                          join tg in blah
                          where (someConditionIsTrueOk)
                          select tg).ToList();


        var query2 = (from ta in this.Context.TableA
                          join tb in this.Context.TableB on ta.Id equals tb.Id
                          join tc in blah
                          join td in blah
                          join te in blah
                          join tf in blah
                          join tg in blah
                          where (someConditionIsTrueOk)
                          select te).ToList();

最佳答案

您可以选择多个表或一个表中的列,然后将它们分组到一个匿名类中,然后该类立即创建:

var result = (from ta in this.Context.TableA
              join tb in this.Context.TableB on ta.Id equals tb.Id
              ...
              ...
              where (someConditionIsTrueOk)
              select new {ta, tb}).ToList();

var firstTableA = result[0].ta;
var firstTableB = result[0].tb;

09-25 17:31
查看更多