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

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

什么是复杂结果集?

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

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

而前者的结果集依赖。

image

代码如下:

namespace LINQ导航字段设计及返回实名类型
{
    class Program : LinqTestBase
    {
        static void Main(string[] args)
        {

            new Program().GetUserBaseDetail().ToList().ForEach(item =>
            {
                Console.WriteLine("用户:" + item.Name + ",真实姓名" + item.UserInfos_Extend.RealName);

                if (item.UserInfos_Extend.UserAddress_Extend != null && item.UserInfos_Extend.UserAddress_Extend.Count > 0)
                {
                    Console.WriteLine("用户常用地址为:");
                    item.UserInfos_Extend.UserAddress_Extend.ForEach(i =>
                    {
                        Console.WriteLine(i.Address);
                    });
                }
            });
            Console.ReadKey();
        }

        IQueryable<UserBases> 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<UserInfos> 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(),
                       };
            return linq;

        }
    }
}

posted @ 2012-01-16 14:11  张占岭  阅读(701)  评论(0编辑  收藏  举报