惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
Last Week in AI
Last Week in AI
V
V2EX
博客园_首页
IT之家
IT之家
Jina AI
Jina AI
博客园 - 叶小钗
The Cloudflare Blog
T
Tailwind CSS Blog
腾讯CDC
B
Blog
D
Docker
L
LangChain Blog
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI

博客园 - 龙少爷

将博客搬至CSDN 偶遇 Lc.exe已退出代码为-1 安装CentOS时,显示 NET:Registered protocol family 2 Nunit2.6.2调试.net4类库 清除SqlServer2008的日志 DomainDataSource的自动刷新 使用WCF RIA服务支持ASP.NET验证 使用dataInput:DescriptionViewer对输入的数据进行校验 android程序连接后端web service时,提示:Permission denied 设置XP系统的自动登录 chrome浏览器的迅雷插件 cmd中更换用户权限 使用WCF Ria取得子表的数据,一直为null SilverLight中使用WCF Ria出现的问题empty domain service class 在Java中使用Hibernate,使用不同数据库服务名 将固定字符分隔的内容转换为数组 Flex中TabBar与ViewStack 使用ApdComPort 拖动控件
linq的表关系解决办法
龙少爷 · 2011-10-04 · via 博客园 - 龙少爷

记得以前解决这些关系的时候,头疼的要命,也没得出个完美的办法。今天看这种“延迟加载”和“立即加载”时,发现问题解决了。

                    using (DataContext cont = new DataContext())
                    {
                        cont.Log = Console.Out;
                        DataLoadOptions dl = new DataLoadOptions();
                        dl.LoadWith<UserInformation>(p => p.SecurityQuestions);
                        dl.LoadWith<UserInformation>(p => p.UsersRoles);
                        cont.LoadOptions = dl;
                        this.dataGrid1.ItemsSource = (from c in cont.UserInformation
                                                     select c).ToList<UserInformation>();
                    }
在此代码中使用的DataLoadOptions表示立即加载,而生成的实际代码,就是我们使用的内联接及外联接,上面代码生成的实际指令如下:
SELECT [t0].[id], [t0].[loginname], [t0].[username], [t0].[password], [t0].[email_address], [t0].[locked], [t0].[locked_date], [t0].[last_login_date], [t0].[created_date], [t0].[ip_address], [t0].[security_question], [t0].[security_answer], [t0].[online], [t2].[id] AS [id2], [t2].[user_id], [t2].[role_id], (
    SELECT COUNT(*)
    FROM [dbo].[users_roles] AS [t3]
    WHERE [t3].[user_id] = [t0].[id]
    ) AS [value], [t1].[id] AS [id3], [t1].[question]
FROM [dbo].[users] AS [t0]
INNER JOIN [dbo].[security_questions] AS [t1] ON [t1].[id] = [t0].[security_question]
LEFT OUTER JOIN [dbo].[users_roles] AS [t2] ON [t2].[user_id] = [t0].[id]
ORDER BY [t0].[id], [t1].[id], [t2].[id]