如何将 Linq 与 Dataset.xsd 文件一起使用?
如何将 Linq 与 Dataset.xsd 文件一起使用?
我研究过 Linq-to-Datasets 和 Linq-to-XSD,但它们似乎并不能直接与 Visual Studio DataSet.xsd 文件一起使用。
编辑:我实际上找到了一个很好的链接:链接文本 但我似乎不知道如果我想查询相关表该怎么办。
Dim taFields As New TestDSTableAdapters.FieldTableAdapter()
Dim fields = taFields.GetData()
Dim results = From f In fields Select f
For Each field In results
Response.Write(field.FieldID & " " & field.Label & " " & field.FieldTypeRow.FieldTypeLabel)
Next
我收到“对象引用未设置到对象实例”。 运行上述代码时出错,因为我猜, field.FieldTypeRow.FieldTypeLabel 实际上并不是数据查询的一部分。 我是否必须创建一个也从该数据返回数据的方法? 我假设情况就是这样(如果我要引用 taFields.GetData,它也必须返回所有 FieldType 数据结果 - 在 Linq to SQL 中,它为我完成了所有这些工作,所以有点令人失望)
How can I use Linq with Dataset.xsd files?
I've looked at Linq-to-Datasets and Linq-to-XSD but they don't really seem to work directly with the Visual Studio DataSet.xsd files.
EDIT: I actually found a great link for this: link text but I can't seem to figure out what to do if I want to query on related tables.
Dim taFields As New TestDSTableAdapters.FieldTableAdapter()
Dim fields = taFields.GetData()
Dim results = From f In fields Select f
For Each field In results
Response.Write(field.FieldID & " " & field.Label & " " & field.FieldTypeRow.FieldTypeLabel)
Next
I get an "Object reference not set to an instance of an object." error when running the above code because, I guess, field.FieldTypeRow.FieldTypeLabel isn't actually part of the query of data. Do I have to create a method that returns data from that data also? I'm assuming this would be the case (that taFields.GetData has to return all the FieldType data results too if I'm going to reference it - in Linq to SQL it does all this for me so it's a little disappointing)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DataSet 只是数据的容器,因此您需要先填充它。 LINQ to SQL 将为您创建 SQL 并转到数据库...但是当您使用数据集时,您使用的是 LINQ to Objects,这不会'创建 SQL。 因此,在开始编写 LINQ 来查询 DataSet 之前,您需要确保 DataSet 中所需的所有表均已填充。
我认为您正在寻找以下内容:
A DataSet is just a container for your data, so you need to fill it first. LINQ to SQL will create SQL and go to the database for you...but when you're working with DataSets, you're using LINQ to Objects, which won't create SQL. So you need to make sure that all tables you need in the DataSet are filled before you start writing LINQ to query your DataSet.
I think you're looking for something along these lines:
您忘记设置字段的类型。 这就是您遇到对象引用问题的原因。 您不必创建新的空白数据集。
You forgot to set the type for fields. That is why yo uwere getting object reference problems. You do not have to create a new blank dataset.