使用Join而不影响数据计数

时间:2014-08-05 06:46:45

标签: c# mysql sql linq join

我有一个问题,我需要使用Linq加入我的Controller中的表,但是当我加入表时,数据会受到影响,其中一些被删除或不包括在内,我认为我的Join正在过滤表并减少使用Linq

获取的数据

我会使用什么类型的加入?交叉连接,左外连接或右连接?

   WHAT I AM GETTING: 
                              POSITION TABLE                         
    POSITION_ID |Location_id|POSITION _Name|  APPLIED or NOT (buttons) | 
          1     |    1      |  DRIVER      |               YES         |
          3     |    3      |  NURSE       |               YES         |
          4     |    4      |  POLICE      |               YES         |

    DESIRED OUTPUT: 

                              POSITION TABLE                         
    POSITION_ID |Location_id|POSITION _Name|  APPLIED or NOT (buttons) | 
          1     |    1      |  DRIVER      |               YES         |
          2     |    2      |  ANALYSTS    |               NO          |
          3     |    3      |  NURSE       |               YES         |
          4     |    4      |  POLICE      |               YES         |
          5     |    5      |  DOCTOR      |               NO          |

我希望有人可以帮助我

这是我的Linq声明

var newPositionList = (from a in db.Position
                 join b in db.Position_Location
                 on a.ID equals b.PositionId            
                 join c in db.Employer
                 on a.EmployerID equals c.ID
                 join d in db.EmployerLocation
                 on b.LocationId equals d.ID
                 join e in db.WorkersApplied    <--------------- Joined Table
                 on a.ID equals e.PositionID
                 where b.LogicalDelete == false
                 && a.LogicalDelete == false
                 && c.LogicalDelete == false
                 && d.LogicalDelete == false
                 select new
                 {
                     a.ID,
                     CompanyName = c.Company_Name,
                     ClientId = c.ClientID,
                     EmployerID = c.ID,
                     LocationId = b.LocationId,
                     e.LogicalDelete

                 }).Distinct().ToList();

            var PositionInfoListNew = new List<PositionInfo>();

            foreach (var row in newPositionList)
            {
                var pos = new PositionInfo
                {
                    ID = row.ID,
                    ClientID = row.ClientId,
                    userId = row.userId.ToString(),
                    EmployerID = row.EmployerID,
                    Company = row.CompanyName,
                    LocationId = row.LocationId,
                    LogicalDelete = row.LogicalDelete
                };
                PositionInfoListNew.Add(pos);
            }


            return Json(PositionInfoListNew.ToList(), JsonRequestBehavior.AllowGet);

0 个答案:

没有答案