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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 黃偉榮

Web Project的檔案共用小技巧 IoC的中繼器:CommonServiceLocator UTF8Encoding與BOM Temporary Post Used For Theme Detection (d4b0aefa-c88e-4957-bba7-b367d1bfa042 - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 寫CodedUI時如何尋找控制項的小技巧 Visual Studio 2010 Feature Packs 2之Silverlight自動化測試 Moles - Isolation framework for .NET(假.Net)介紹 C#仿Oracle Decode,將ValueType對應成String - 黃偉榮 - 博客园 Visual Studio 單元測試的3種Initialize與Cleanup jQuery套件-檢查頁面的欄位是否有變更 用EventLogReader查詢特殊EventLog jQuery自製Plugin-Bind事件函式時檢查有沒有Bind過 ASP.NET MVC TempData使用心得 Visual Stuiod 自訂檔案比較合并工具 [小技巧]自動化測試時NLog的訊息輸出到測試結果中 小技巧:專案切換32與64位元組件 Linq小技巧:日期處理 Unit Test小技巧 : DateTime的Stub 解決TFS Build Asp.Net Mvc開啟MvcBuildViews後無法載入組件問題
[小技巧]Entity Framework強型別Include
黃偉榮 · 2010-10-20 · via 博客园 - 黃偉榮

在Entity Framework中如果不設定為Lazy Loading Enable=true,勢必要自己處理關聯資料的載入,如:用Load或Include的,但因為Include的參數是用string,個人很討厭沒有IntelliSense,且用String改了TableName後,工具不易找到錯誤,所以小弟我寫一個Extension Method來擴展。

//原弱型別的寫法
AdventureWorksLT2008R2Entities context = new AdventureWorksLT2008R2Entities();
var customer = context.Customer.Include("SalesOrderHeader").ToList();

//擴展的強型別寫法
AdventureWorksLT2008R2Entities context = new AdventureWorksLT2008R2Entities();
var customer = context.Customer.Include(x => x.SalesOrderHeader).ToList();

NOTE:

這個方法暫時不適用Collection後又在關聯,如上一個例子SalesOrderHeader是Collection,無法這樣下x.SalesOrderHeader.SalesOrderDetail,為什麼是暫時,因為小弟還沒想出好的寫法,用x.SalesOrderHeader[0].SalesOrderDetail或x.SalesOrderHeader.First().SalesOrderDetail嗎,感覺挺醜的。

Source Code

public static IQueryable<T> Include<T>(this ObjectSet<T> source, Expression<Func<T, object>> path) where T : class
{
    //偷吃步的作法,如:Expression為x=>x.Customer.CustomerAddress,ToString後直接從第一個.分割,取後面的Customer.CustomerAddress
    string spath = path.Body.ToString();
    spath = spath.Substring(spath.IndexOf('.') + 1);
    return source.Include(spath);
}

上面的Code比較偷懶,應該要分析一下Expression比較正統

public static IQueryable<T> Include<T>(this ObjectSet<T> source, Expression<Func<T, object>> path) where T : class
{
    StringBuilder pathBuilder = new StringBuilder();

    MemberExpression pro = path.Body as MemberExpression;
    while (pro != null)
    {
        //Exprssion有點像鏈結串列,從Statemant的後方往前連結,如: x=> x.Customer.CustomerAddress
        //path.Body是CustomerAddress
        //CustomerAddress的Expression是Customer
        //Customer的Expression是x
        pathBuilder.Insert(0, "." + pro.Member.Name);
        pro = pro.Expression as MemberExpression; 
    }

    return source.Include(pathBuilder.ToString(1, pathBuilder.Length-1));
}

NOTE:

我沒有做錯誤檢查,想說有錯Entity Framework也會丟Exception,自己就不多事了。

圖1 Model關聯圖

image