正确加入Linq,没有返回正确的值

时间:2017-05-08 20:34:28

标签: c# sql linq

我正在尝试构建此linq查询,但我遇到了问题

我有两个列表

ROOMS,其中包含 Id,Name,RoomStatus,OrderStatus,ConsructionType

STRINGMAP,其中包含 AttributeName,AttributeValue,Value

var stringMap = from sm in _CRMcontext.StringMaps
                where sm.ObjectTypeCode == 10001 
                && sm.AttributeName == "new_status" 
                || sm.AttributeName == "new_projecttype" 
                || sm.AttributeName == "new_orderstatus" 
                || sm.AttributeName == "new_construction_type" 
                || sm.AttributeName == "new_stage"
                select sm;

var result = from set in ROOMS
             join roomStatus in stringMap on set.Status equals status.AttributeValue
             where roomStatus.AttributeName == "new_status"
             join orderStatus in stringMap on set.OrderStatus equals orderStatus.AttributeValue
             where orderStatus.AttributeName == "new_orderstatus"
             join consructionType in stringMap on set.ConstructionType equals consructionType.AttributeValue
             where consructionType.AttributeName == "new_construction_type"
             select new DTO.Shared.Project() {
                 Id = set.Id,
                 Name = set.Name,
                 RoomStatus = roomStatus.Value,                 
                 OrderStatus = orderStatus.Value,
                 ConstructionType = constructionType.Value,
                 };

所以我只回到连接中没有空值的结果,我想让它们无论是否为null,类似于sql中的右连接

由于 迈克尔

1 个答案:

答案 0 :(得分:0)

以下是Right Outer Join的IQueryable实现:

var result = ROOMS.RightOuterJoin(stringMap, set => new { set.Status, AttributeName = "new_status" }, roomStatus => new { Status = roomStatus.AttributeValue, roomStatus.AttributeName }, (set, roomStatus) => new { set.Id, set.Name, roomStatus.Value });

你可以使用它:

calc()