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

推荐订阅源

GbyAI
GbyAI
B
Blog
Stack Overflow Blog
Stack Overflow Blog
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
D
DataBreaches.Net
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
罗磊的独立博客
L
LangChain Blog
V
Visual Studio Blog
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
宝玉的分享
宝玉的分享

博客园 - 龙少爷

将博客搬至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]