使用 LINQ to SQL 时,我可以将本地数据的值与数据库返回的值混合吗?

发布于 2024-09-29 15:06:42 字数 1454 浏览 3 评论 0原文

我正在使用 LINQ 创建一个 xml 文件,如下所示...

   Public Sub CreateXml()
        Dim db As New MDataContext
        Dim Customers = <gallery columns="3" rows="3">
                            <%= From customer In db.Customers _
                                Select <customer>
                                           <name><%= customer.CustName %></name>
                                           <surname><%= customer.Surname %></surname>
                                       </customer> %>
                        </gallery>
        Customers.Save("d:\1.xml")
    End Sub

我可以将本地值与从 LINQ 查询返回的值混合在一起吗... 像下面这样的东西吗?

 Public Sub CreateXml(ByVal **Col** As String, ByVal **Row** As String)
        Dim db As New MDataContext
        Dim Customers = <gallery columns="& **Col** &" rows="& **Row** &">
                            <%= From customer In db.Customers _
                                Select <customer>
                                           <name><%= customer.CustName %></name>
                                           <surname><%= customer.Surname %></surname>
                                       </customer> %>
                        </gallery>

        Customers.Save("d:\1.xml")
        Process.Start("d:\1.xml")
    End Sub

I am creating an xml file with LINQ as follows...

   Public Sub CreateXml()
        Dim db As New MDataContext
        Dim Customers = <gallery columns="3" rows="3">
                            <%= From customer In db.Customers _
                                Select <customer>
                                           <name><%= customer.CustName %></name>
                                           <surname><%= customer.Surname %></surname>
                                       </customer> %>
                        </gallery>
        Customers.Save("d:\1.xml")
    End Sub

Could i mix local values with the ones returned from the LINQ query...
Something like the following?

 Public Sub CreateXml(ByVal **Col** As String, ByVal **Row** As String)
        Dim db As New MDataContext
        Dim Customers = <gallery columns="& **Col** &" rows="& **Row** &">
                            <%= From customer In db.Customers _
                                Select <customer>
                                           <name><%= customer.CustName %></name>
                                           <surname><%= customer.Surname %></surname>
                                       </customer> %>
                        </gallery>

        Customers.Save("d:\1.xml")
        Process.Start("d:\1.xml")
    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2024-10-06 15:06:42

您应该能够嵌入这两个变量的值,就像处理 customer.CustName 和 customer.Surname 一样,例如

Public Sub CreateXml(ByVal cols As Integer, ByVal rows As Integer)
    Dim db As New MDataContext
    Dim Customers = <gallery columns="<%= cols %>" rows="<%= rows %>">
                        <%= From customer In db.Customers _
                            Select <customer>
                                       <name><%= customer.CustName %></name>
                                       <surname><%= customer.Surname %></surname>
                                   </customer> %>
                    </gallery>

    Customers.Save("d:\1.xml")
    Process.Start("d:\1.xml")
End Sub

严格来说,您应该进行适当的 ToString() 调用,例如<%= cols.ToString(CultureInfo.InvariantCulture) %>。通过这种方式,您可以确保更改不会引入任何不必要的装箱,并且也独立于执行线程的区域性,尽管后者不太可能对 Integer 变量产生任何影响。

You should be able to embed the values of the two variables just as you are doing with the customer.CustName and customer.Surname, e.g.

Public Sub CreateXml(ByVal cols As Integer, ByVal rows As Integer)
    Dim db As New MDataContext
    Dim Customers = <gallery columns="<%= cols %>" rows="<%= rows %>">
                        <%= From customer In db.Customers _
                            Select <customer>
                                       <name><%= customer.CustName %></name>
                                       <surname><%= customer.Surname %></surname>
                                   </customer> %>
                    </gallery>

    Customers.Save("d:\1.xml")
    Process.Start("d:\1.xml")
End Sub

Strictly speaking, you should probably make an appropriate ToString() call, e.g. <%= cols.ToString(CultureInfo.InvariantCulture) %>. This way you ensure that the change doesn't introduce any unnecessary boxing and is also independent of the culture of the executing thread, although the latter is not likely to have any affect with an Integer variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文