使用 LINQ 从数据集中选择单列

发布于 2024-10-07 13:46:20 字数 451 浏览 0 评论 0原文

刚开始思考所有这些 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 技术交流群。

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

发布评论

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

评论(3

第几種人 2024-10-14 13:46:20

有一些扩展方法可以使数据集的处理变得更加容易:

using System.Data.Linq;

var filePaths =
    from row in dataTable.AsEnumerable()
    select row.Field<string>("Filepath");

var filePathsArray = filePaths.ToArray();

您还可以使用方法语法将其放入一条语句中:

var filePaths = dataTable
    .AsEnumerable()
    .Select(row => row.Field<string>("Filepath"))
    .ToArray();

There are extension methods which make working with data sets much easier:

using System.Data.Linq;

var filePaths =
    from row in dataTable.AsEnumerable()
    select row.Field<string>("Filepath");

var filePathsArray = filePaths.ToArray();

You can also use the method syntax to put it in one statement:

var filePaths = dataTable
    .AsEnumerable()
    .Select(row => row.Field<string>("Filepath"))
    .ToArray();
懒的傷心 2024-10-14 13:46:20
string[] filePaths = (from DataRow row in yourDataTable.Rows 
                     select row["Filepath"].ToString()).ToArray();
string[] filePaths = (from DataRow row in yourDataTable.Rows 
                     select row["Filepath"].ToString()).ToArray();
吲‖鸣 2024-10-14 13:46:20

如果您想完全使用 LINQ,请设置数据库并创建上下文对象。那么您应该能够执行以下操作:

 var filepaths = from order in _context.Orders
                 select order.Filepath;

假设您的行表名为 Orders,我猜是您的订单的第一个列名称。如果您还想返回一组订单号以便稍后使用来了解文件路径的来源,您可以这样做:

var results = from order in _context.Orders
              select new
              {
                  order.OrderNo,
                  order.Filepath
              }

这将为您提供一个新的匿名类型,其中包含这两个值作为属性。

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:

 var filepaths = from order in _context.Orders
                 select order.Filepath;

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:

var results = from order in _context.Orders
              select new
              {
                  order.OrderNo,
                  order.Filepath
              }

This would give you a new anonymous type that contained both those values as properties.

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