使用 LINQ 从数据集中选择单列
刚开始思考所有这些 LINQ 的东西,我似乎陷入了第一个障碍。
我有一个这样的数据表:
OrderNo LetterGroup Filepath
----------- ----------- --------------------------------------------------
0 0 Letters/SampleImage.jpg
0 0 Letters/UKPC7_0.jpg
0 0 Letters/UKPC8_0.jpg
我需要的是将文件路径列中的所有文件路径获取到字符串数组中。我认为 LINQ 非常适合这个(我是对的吗?),但似乎无法构建正确的查询。
谁能提供一些代码示例来为我指明正确的方向?我已经四处寻找 - 但似乎一无所获。
Just getting my head around all this LINQ stuff and it seems I'm stuck at the first hurdle.
I have a datatable as such:
OrderNo LetterGroup Filepath
----------- ----------- --------------------------------------------------
0 0 Letters/SampleImage.jpg
0 0 Letters/UKPC7_0.jpg
0 0 Letters/UKPC8_0.jpg
What I need is to get all of the filepaths from the Filepath column into a String array. I thought LINQ would be perfect for this (am I right?), but can't seem to construct the correct query.
Can anyone provide some code samples that would point me in the right direction? I have searched around - but don't seem to be getting anywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一些扩展方法可以使数据集的处理变得更加容易:
您还可以使用方法语法将其放入一条语句中:
There are extension methods which make working with data sets much easier:
You can also use the method syntax to put it in one statement:
如果您想完全使用 LINQ,请设置数据库并创建上下文对象。那么您应该能够执行以下操作:
假设您的行表名为 Orders,我猜是您的订单的第一个列名称。如果您还想返回一组订单号以便稍后使用来了解文件路径的来源,您可以这样做:
这将为您提供一个新的匿名类型,其中包含这两个值作为属性。
If you want to use LINQ all the way, set up your database and create a context object. Then you should be able to do something like this:
This is assuming your table for the row is named Orders, which I guess by your first column name of order. If you wanted to return a set of the order numbers as well for using later to know where the file path came from you could do something like so:
This would give you a new anonymous type that contained both those values as properties.