Linq:连接两张桌子

本文关键字:两张 连接 Linq | 更新日期: 2023-09-27 18:17:09

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

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;
}

但是映射规则列抛出错误。谁能帮我一下?

Linq:连接两张桌子

解决方案:

      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