天天看点

谁说LINQ复杂查询不支持返回实名类型~复杂结果集中再使用复杂结果集

下面代码主要是从一个复杂结果集中进行一个查询,在查询中使用的结果集还是一个复杂结果集。

什么是复杂结果集?

这是我给它的定义,它是由多张表进行关联查询后,生成的新的对象结果集或者原来结果集。如下图所示

GetUserInfoDetail()方法会产生一个复杂结果集,而GetUserBaseDetail()同样会产生一个复杂结果集,后者的结果集

而前者的结果集依赖。

<a href="http://images.cnblogs.com/cnblogs_com/lori/201201/201201161411146965.png"></a>

代码如下:

namespace LINQ导航字段设计及返回实名类型

{

    class Program : LinqTestBase

    {

        static void Main(string[] args)

        {

            new Program().GetUserBaseDetail().ToList().ForEach(item =&gt;

            {

                Console.WriteLine("用户:" + item.Name + ",真实姓名" + item.UserInfos_Extend.RealName);

                if (item.UserInfos_Extend.UserAddress_Extend != null &amp;&amp; item.UserInfos_Extend.UserAddress_Extend.Count &gt; 0)

                {

                    Console.WriteLine("用户常用地址为:");

                    item.UserInfos_Extend.UserAddress_Extend.ForEach(i =&gt;

                    {

                        Console.WriteLine(i.Address);

                    });

                }

            });

            Console.ReadKey();

        }

        IQueryable&lt;UserBases&gt; GetUserBaseDetail()

            var linq = from data1 in db.UserBases

                       join data2 in GetUserInfoDetail() on data1.UserID equals data2.UserID

                       select new UserBases_Ext

                      {

                          UserID = data1.UserID,

                          Name = data1.Name,

                          UserInfos_Extend = data2,

                      };

            return linq;

        IQueryable&lt;UserInfos&gt; GetUserInfoDetail()

            var linq = from data1 in db.UserInfos

                       join data3 in db.UserAddress on data1.UserID equals data3.UserID into list

                       select new UserInfos_Ext

                       {

                           UserID = data1.UserID,

                           RealName = data1.RealName,

                           UserAddress_Extend = list.ToList(),

                       };

    }

}

继续阅读