开发者

C# lambda expression question - how to join 2 tables using Lambda statements from the following SQL? [closed]

开发者 https://www.devze.com 2023-03-11 00:38 出处:网络
开发者_StackOverflow It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current
开发者_StackOverflow It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have 2 tables, and I would like to join them using Lambda statements (not Linq but Lambda).

This is the query that I need:

SELECT
    c.*
FROM
    board as b
LEFT JOIN category as c ON
    b.cid = c.cid
WHERE
    b.bid = 1

How do I do this?

Say if board is a dataset/variable and category is an other dataset/variable then i want somethign like board.Join(category).Where(b=>b.bid==c.cid) ( i know this is wrong but just so you have an idea what i am looking for thank you so much for all your help


If you mean method syntax and not query syntax of linq then you can do

var results = context.boards.Where(b => b.bid == 1)
                            .DefaultIfEmpty()
                            .Join(context.categories, 
                                  b => b.bid,
                                  c => c.cid,
                                  (b, c) => c);


You can use a group join like:

var qry = boards.GroupJoin(
    categories,
    b => b.CategoryID,
    c => c.CategoryID,
    (x, y) => new { Board = x, Categories = y })
    .SelectMany(
    x => x.Categories.DefaultIfEmpty(),
    (x, y) => new { Board = x.Board, Category = y });
0

精彩评论

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

关注公众号