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

fra*_*kie 5 .net c# sql linq vb.net

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

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
Run Code Online (Sandbox Code Playgroud)

与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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SCowomerInfo由下面的代表Structure(代码简化):

Public Structure SWindAltitude
    Public PaymentTableAs DataTable
    Public Title As String
    Public ID As Integer
End Structure
Run Code Online (Sandbox Code Playgroud)

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

Moh*_*oho 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;
        } );
Run Code Online (Sandbox Code Playgroud)

  • 枚举数据库中的结果 - LINQ to Entities select 语句中不能有带参数的构造函数。调用“ToArray()”从数据库获取结果,我们现在使用“IEnumerable&lt;T&gt;.Select(...)”。如果没有 `.ToArray()`,我们仍然会使用 L2E `IQueryable&lt;T&gt;` (2认同)