将两个列表合并为一个

时间:2016-09-11 09:56:13

标签: c# entity-framework linq

我目前有两个列表,每个列表中的字段不同,一个称为付款,另一个称为贡献

public List<tblPortalContributionSchedule> getUserContributions(Guid _customerInfoId)
    {
        List<tblPortalContributionSchedule> _contributions = portalEntities.tblPortalContributionSchedules.Where(a => a.customerInfo == _customerInfoId && a.isDeleted == false).ToList();

        return _contributions;
    }

这是第二个列表,它有一个名为date的字段,我想将第一个列表限制为小于或等于但我想将这两个查询合并到一个列表中,因为我正在使用转发器来显示信息I提供我的html以下,希望有人可以帮我把这两个陈述合二为一。

public List<tblPortalPayment> getUserPayments(Guid recordId)
    {

        List<tblPortalPayment> _payments = portalEntities.tblPortalPayments.Where(a => a.CustomerId == recordId && a.isDeleted == false).ToList();

        return _payments;

    }

转发器的Html代码

 <asp:Repeater ID="rptPayments" runat="server" OnItemDataBound="rptPayments_ItemDataBound">
                            <ItemTemplate>



                                <div class="panel panel-default">
                                    <div class="panel-heading pointer" data-toggle="collapse" data-parent="#accordion" href="#collapse<%#Container.ItemIndex %>">

                                        <div class="alert-box notice">
                                            <h4 class="panel-title">
                                                <asp:HiddenField ID="isinArrears" runat="server" Value='<%#Eval("isinArrears") %>' />
                                                <a><%# Convert.ToDateTime(Eval("paymentDate")).ToString("MMMM d  yyyy") %>  </a>
                                                <asp:Label ID="lblStatus" runat="server" CssClass="fa fa-info-circle"></asp:Label>
                                            </h4>
                                        </div>

                                    </div>
                                    <div id="collapse<%#Container.ItemIndex %>" class="<%# GetItemClass(Container.ItemIndex) %>" style="height: auto;">
                                        <div class="panel-body center">

                                            <table id="paymentsummaryhistory">
                                                <tbody>
                                                    <tr>
                                                        <td><strong>Payment Agreed: </strong></td>
                                                        <td><strong>Payment Date:  </strong></td>

                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <dx:ASPxLabel ID="lblPaymentDUE" runat="server" Text='<%# "£" + Eval("paymentDue") %>'></dx:ASPxLabel>
                                                            <br />
                                                        </td>
                                                        <td>
                                                          <dx:ASPxLabel ID="lblPayemntDate" runat="server" Text='<%# Convert.ToDateTime(Eval("paymentDate")).ToString("dd/MM/yyyy") %>'></dx:ASPxLabel>
                                                        </td>


                                                    </tr>
                                                </tbody>
                                            </table>

                                            <dx:ASPxLabel ID="lblArrearsInfo" ClientVisible="False" runat="server" Text='<%# Eval("isinArrears") %>'></dx:ASPxLabel>

                                        </div>
                                    </div>
                                </div>
 </ItemTemplate>
 </asp:Repeater>

修改1

我稍微修改了我的查询,因为我只想要前5个贡献,因为6个付款是在标题记录中所以它可以帮助我有两个不同的字段首先linq我想要paymentDue和第二个linq我想要贡献,但我想将这些组合成一个列表,因此可以绑定到转发器

public List<tblPortalContributionSchedule> getUserContributions(Guid _customerInfoId,DateTime _date)
    {
        List<tblPortalContributionSchedule> _contributions = portalEntities.tblPortalContributionSchedules.Where(a => a.customerInfo == _customerInfoId && a.isDeleted == false && a.DUE_DATE <= _date && a.DUE_DATE !=_date ).Take(5).ToList();

        return _contributions;
    }

修改2

属性贡献计划 我需要这个日期和贡献 https://snag.gy/PhJgsA.jpg

物业付款 我要求paydue和paymentdate来自这个

https://snag.gy/3wWEjb.jpg

我需要的唯一值是截止日期

2 个答案:

答案 0 :(得分:1)

好的所以我得到了我的答案,但是我知道有一种更简洁的方法可以做到这一点。

public List<Payments> getAllPayemtns(Guid _customerId)

    {
        List<Payments> AllPayments = new List<Payments>();

        List<tblPortalPayment> _payments = portalEntities.tblPortalPayments.Where(a => a.CustomerId == _customerId && a.isDeleted == false).ToList();

        foreach (var payment in _payments)
        {
            AllPayments.Add(new Payments(payment.id, Convert.ToDecimal(payment.paymentDue), Convert.ToDateTime(payment.paymentDate), Convert.ToBoolean(payment.isinArrears));

        }

        List<tblPortalContributionSchedule> _contributions = portalEntities.tblPortalContributionSchedules.Where(a => a.customerInfo == _customerId && a.isDeleted == false && a.DUE_DATE <= _date && a.DUE_DATE != _date).Take(6).OrderByDescending(o => o.DUE_DATE).ToList();
        foreach (var contribution in _contributions)
        {

            AllPayments.Add(new Payments(contribution.ID, Convert.ToDecimal(contribution.Contribution), Convert.ToDateTime(contribution.DUE_DATE), false));

        }

 return AllPayments;
}

然后你有付款类

public class Payments
    {
        public Guid? id { get; set; }
           public Decimal paymentDue { get; set; }
        public DateTime paymentDate { get; set; }
         public bool isinArrears { get; set; }


        public Payments(Guid? id, Decimal paymentDue, DateTime paymentDate, 
         bool isinArrears)
        {
            this.id = id;
             this.paymentDue = paymentDue;
            this.paymentDate = paymentDate;
              this.isinArrears = isinArrears;

        }
}

答案 1 :(得分:1)

希望你喜欢这个:) 注意:请根据您的型号正确调整型号属性。 var finalResult = _payments.Select(p =&gt; new Payments {DueDate = p。 paymentdue})。Concat(_contributions.Select(c =&gt; new Payments {DueDate = c.dueDate});