Left join in Linq?

后端 未结 3 1591
长发绾君心
长发绾君心 2020-12-16 06:52

There are a lot of questions on SO already about Left joins in Linq, and the ones I\'ve looked at all use the join keyword to achieve the desired end.

T

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 07:20

    What are you talking about? That from i in c.Invoice.DefaultIfEmpty() is exactly a left join.

            List strings = new List() { "Foo", "" };
    
            var q = from s in strings
                    from c in s.DefaultIfEmpty()
                    select new { s, c };
    
            foreach (var x in q)
            {
                Console.WriteLine("ValueOfStringIs|{0}| ValueOfCharIs|{1}|",
                    x.s,
                    (int)x.c);
            }
    

    This test produces:

    ValueOfStringIs|Foo| ValueOfCharIs|70|
    ValueOfStringIs|Foo| ValueOfCharIs|111|
    ValueOfStringIs|Foo| ValueOfCharIs|111|
    ValueOfStringIs|| ValueOfCharIs|0|
    

提交回复
热议问题