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

C# 如何将两个序列合并为一个?,c#,.net,sql,vb.net,linq,C#,.net,Sql,Vb.net,Linq,我有一些从数据库检索数据的工作代码。对于我来说,为我的解决方案获得一些更好的代码是很有趣的。有没有办法将两个查询合并成一个或类似的查询 Dim customerTitlesAndIDs = contex.CustomerTable.Select(Function(row) New With {.ID = row.ID, .CustomerTitle = row.Title}).ToList() Dim cutomerIdPayment =

我有一些从数据库检索数据的工作代码。对于我来说,为我的解决方案获得一些更好的代码是很有趣的。有没有办法将两个查询合并成一个或类似的查询

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相同的代码,希望没有出现语法错误:


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

尝试类似的方法,利用导航属性,您可能需要对其进行处理,因为我不知道您的数据结构的确切组成:

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

如果我对linq的c语言理解正确,它将是这样的

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();

我想你可以使用Linq提供的函数Sequence.concat,如下所述:

也许只是我自己的问题,但我觉得如果你接受C答案,你也应该发布C代码,因为我不太精通VBC。你认为将C和VB.net代码放在一个问题中会很好吗?C和VBNET可以被视为一个问题,虽然两者都依赖于.NET Framework,而且两种语言都可以很容易地在两者之间进行翻译,但对于我来说,如果有人在特定语言的.NET问题中同时要求C/VBNET解决方案,这是正确的。但有些。。。人们喜欢否决那些愚蠢的事情或删除标记。使用JOIN-contex.CustomerTable.Joincontex.CustomerPayments,…可以显示一些示例代码吗?为什么在选择之前调用Array?要枚举DB的结果-不能在LINQ-to-Entities-Select语句中使用带参数的构造函数。调用ToArray从数据库中获取结果,我们现在使用IEnumerable.Select。。。。如果没有。ToArray,我们仍然会使用L2E iQueryTable。SCustomerInfo的构造函数和函数CreateCustomerTable可能会被重新分解以使用L2E,具体取决于它们的功能。我很想了解如何使用,@pseudo coder。它使用非无参数构造函数实例化客户端对象。我很确定这在L2E中是不可能的-您将得到唯一允许的无参数构造函数异常message@Moho我的意思是,可能只使用无参数构造函数和对象初始值设定项就可以完成工作,具体取决于它们所做的工作。显然,如果不查看源代码,我们就无法判断。我怀疑新的scustomernofx,x,x可以从LINQ to Entities查询中调用,就像您在这里看到的那样;见@Moho答案。
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;
        } );
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();