开发者

In VB.NET, which LINQ to SQL Left-Join method is best to use?

开发者 https://www.devze.com 2023-03-24 03:59 出处:网络
Is one of these VB.NET LINQ to SQL Left Join options superior to other? I\'m not sure which to use. Method 1: Lambda

Is one of these VB.NET LINQ to SQL Left Join options superior to other?

I'm not sure which to use.

Method 1: Lambda

Dim query = From A In DB.Product_Categories
            From B In DB.MasItems.Where(Function(x) CBool(x.itemkey = A.ItemKey)).DefaultIfEmpty
            Select A.Name

Method 2: Group Join

Dim query = From A In DB.Product_Categories
            Group Joi开发者_开发百科n B In DB.MasItems On B.itemkey Equals A.ItemKey Into X = Group
            From Y In X.DefaultIfEmpty
            Select A.Name()


The generated SQL is the same for both queries, so in LINQ to SQL it is really personal preference.


EDIT: This is true for LINQ to Objects.
I don't know about LINQ to SQL.

Method 2 is faster, since GroupJoin builds a hashtable internally.

Method 1 is O(n2), since it needs to search all of MasItems for each category.

0

精彩评论

暂无评论...
验证码 换一张
取 消