如何将两个序列合并为一个?

时间:2013-11-26 14:09:19

标签: c# .net sql vb.net linq

我有一些可以从数据库中检索数据的工作代码。我有兴趣为我的解决方案获得更好的代码。有没有办法将两个查询组合成一个或类似的东西?

Dim customerTitlesAndIDs = contex.CustomerTable.Select(Function(row) New
                           With {.ID = row.ID, .CustomerTitle = row.Title}).ToList()

Dim cutomerIdPayment = contex.CustomerPayments.Select(Function(table) New 
                       With
                       {
                           .ID = table.CustomerID,
                           .Range = table.PaymentsRange,
                           .Values = table.Values
                       }).ToList()

Dim customerInfos As New List(Of SCustomerInfo)

For Each customer In customerTitlesAndIDs

    Dim cID As Integer = customer.ID
    customerInfo.Add(New SCustomerInfo(CreateCustomerTable(), cID, customer.CustomerTitle))

    For Each cutomerPayments In cutomerIdPayment

        If cutomerPayments.ID = cID Then
            Dim rangeValue(1) As Object
            rangeValue(0) = cutomerPayments.Range
            rangeValue(1) = cutomerPayments.Values
            Dim dtRow As DataRow = customerInfos.Last().PaymentTable.NewRow()
            dtRow.ItemArray = rangeValue
            customerInfos.Last().PaymentTable.Rows.Add(dtRow)
        End If
    Next
Next

Return customerInfos

与C#相同的代码(希望没有语法错误发生):

var customerTitlesAndIDs = contex.CustomerTable.Select(row => new
                           { .ID = row.ID, .CustomerTitle = row.Title }).ToList();

var cutomerIdPayment = contex.CustomerPayments.Select(table => new
                       {
                           .ID = table.CustomerID,
                           .Range = table.PaymentsRange,
                           .Values = table.Values
                       }).ToList();

List<SCustomerInfo> customerInfos = new List<SCustomerInfo>;

foreach (var customer in customerTitlesAndIDs)
{
    int cID = customer.ID;
    customerInfos.Add(new SCustomerInfo(CreateCustomerTable(), cID, customer.CustomerTitle));

    foreach (var cutomerPayments in cutomerIdPayment)
    {
        if (cutomerPayments.ID = cID)
        {
            object[] rangeValue = new object[1] {cutomerPayments.Range, cutomerPayments.Values};
            DataRow dtRow = customerInfos.Last().PaymentTable.NewRow();
            dtRow.ItemArray = rangeValue;

            customerInfos.Last().PaymentTable.Rows.Add(dtRow);
        }
    }
}

SCustomerInfo由以下Structure代表(代码已简化):

Public Structure SWindAltitude
    Public PaymentTableAs DataTable
    Public Title As String
    Public ID As Integer
End Structure

C#和VB.NET解决方案都会有所帮助。

3 个答案:

答案 0 :(得分:2)

尝试这样的事情,利用导航属性(你可能不得不按摩它,因为我不知道你的数据结构的确切构成):

var customerQuery = context.CustomerTable.Select( ct => 
    new { 
        ct.ID, 
        ct.CustomerTitle, 
        // use nav property to get customer payments
        CustomerPayments = ct.CustomerPayments.Select( cp => 
            new { 
                Range = cp.Range, 
                Values = cp.Values } ) } );

return customerQuery.ToArray()
    .Select( cq => 
        {
            var retVal = new SCustomerInfo( CreateCustomerTable(), cq.ID, cq.CustomerTitle ); 

            foreach( var customerPayment in cq.CustomerPayments )
            {
                var dtRow = cq.PaymentTable.NewRow();

                dtRow.ItemArray = new object[] { customerPayment.Range, customerPayment.Values };

                retVal.PaymentTable.Rows.Add( dtRow );
            }

            return retVal;
        } );

答案 1 :(得分:0)

如果我在c#中使用linq了解它就会像这样

var customerInfos = customerTitlesAndIDs.Select((c)=>{
                        var ci = new SCustomerInfo(CreateCustomerTable(), c.ID, customer.CustomerTitle);
                        ci.PaymentTable = ci.PaymentTable.AsEnumerable().Union(
                                          cutomerIdPayment.Where(j=>j.ID == c.ID)
                                                          .Select(j=>{
                                                              var dtRow = ci.PaymentTable.NewRow();
                                                              dtRow.ItemArray = new object[] {
                                                                  customerPayment.Range,
                                                                  customerPayment.Values 
                                                              };
                                                              return dtRow;
                                          })).CopyToDataTable();
                        return ci;
                    }).ToList();

答案 2 :(得分:0)

我认为您可以使用Linq提供的函数Sequence.concat(),如下所述:http://msdn.microsoft.com/en-us/library/vstudio/bb386979(v=vs.100).aspx