【问题标题】:Converting SQL to linq with a right join (advanced)使用右连接将 SQL 转换为 linq(高级)
【发布时间】:2014-06-10 02:13:19
【问题描述】:

我有以下 SQL 语句:

    SELECT dh.* 
FROM table1 w
 LEFT OUTER JOIN table2 dh
            on     w.CBranch        = dh.CBranch 
               AND w.CWorkstation   = dh.CWorkstation
               AND w.CNumber        = dh.CNumber               
 RIGHT OUTER JOIN table3 dl
            on     dh.Id = dl.DispatchHeaderId
               AND w.CLine = dl.CLine
               AND w.CLineVersion = dl.CVersion            
    where       w.ItemStatus = 9 
          AND   dh.Shipping = 0 
    ORDER BY dh.CNumber ASC

我是 Linq 的初学者,不知道如何进行高级 linq。

有人可以指导我为此编写等效的 linq。

我正在使用 C#、EF4。

我已经到了这里,但不确定这是否正确。

var wos = scope.Context.table1.Where(
                            a => a.ItemStatus == (short)LineStatus.Packed)
                            .GroupBy(a => new { a.CNumber, a.CBranch, a.CWorkstation})
                            .Select(a => a.FirstOrDefault()).ToList();

            var headerGroups = new List<IEnumerable<table2>>();
            foreach(var status in wos)
            {                    
                if (status == null)
                {
                    continue;
                }

                var headerList = scope.Context.table2s.Where(
                    b => b.CBranch == status.CBranch &&
                        b.CNumber == status.CNumber &&
                        b.CWorkstation == status.CWorkstation).ToList();

                if (headerList != null && headerList.Any())
                {
                    headerGroups.Add(headerList);
                }
            };

【问题讨论】:

标签: c# sql linq


【解决方案1】:

在以下链接方式中使用 defaultifemty() 方法....

http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 2012-03-25
    • 2016-12-19
    • 1970-01-01
    • 2017-08-16
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多