Linq:加入两张桌子

时间:2013-06-06 13:38:45

标签: c# linq

在我的WPF网格上,我填充了以下属性。最后两个来自另一张桌子。我想从另一个表中获取映射规则,其中事务表中的sourceelementid等于id表的messagefield

public List<MessageFieldViewModel> GetAllViewModelMsgFields()
{
    messageFieldVModel = messageField.GetAllMessageField().Select(msgFields => new MessageFieldViewModel
    {
        Id = msgFields.Id,
        Code = msgFields.Code,
        Name = msgFields.Name,
        Position = msgFields.Position,
        Length = msgFields.Length,
        IsMapped = (transactionRuleList.Any(tr=> tr.SourceElementId ==msgFields.Id)),
        MappingRule = transactionRuleList.Where(mapRule => mapRule.MappingRule.Any(tr=> tr.SourceElementId ==msgFields.Id)),
    })
.ToList();
    return messageFieldVModel;
}

但映射规则列会引发错误。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

      transactionRuleList = transationRuleViewModel.GetAllTranslationRules();
        messageFieldVModel = messageField.GetAllMessageField().Select(msgFields => new MessageFieldViewModel
        {
            Id = msgFields.Id,
            Code = msgFields.Code,
            Name = msgFields.Name,
            Position = msgFields.Position,
            Length = msgFields.Length,
            IsMapped = (transactionRuleList.Any(tr => tr.SourceElementId == msgFields.Id)),
            MappingRule = transactionRuleList.Any(mapRule => mapRule.SourceElementId == msgFields.Id)?transactionRuleList.First(mapRule => mapRule.SourceElementId == msgFields.Id).MappingRule:null