简体   繁体   English

从IQueryable检索逗号分隔的值并将其添加到列表

[英]Retrieve comma-separated values from IQueryable and add to list

IQueryable<string> ScannedBarcodes = XYZDb.tblScannedBarcodes
                                          .Where(i => i.PullNo == lblPullNo.Text)
                                          .Select(i => i.Barcode);

The result view shows something like 结果视图显示类似

"678765,090909,243454,675434"

Now I want to add all the comma-separated values into a list. 现在,我要将所有逗号分隔的值添加到列表中。

Rest of my code is below: 我的其余代码如下:

foreach (var item in ScannedBarcodes)
{
    List<string> _barcodes = new List<string>();
    _barcodes.Add(item);
}

How to split it? 如何拆分?

Thanks in advance 提前致谢

The splitting can be done using Linq as follows. 可以使用Linq如下进行拆分。

var _barcodes =
    ScannedBarcodes.SelectMany(iString => iString.Split(new char[]{ ','}))
                   .ToList();

采用

_barcodes.AddRange(item.Split(','));

There's no a single string with commas - that's just the debugger display . 没有一个带逗号的字符串-只是调试器的显示 The IQueryable is a enumerable list of values already..... IQueryable已经是一个可枚举的值列表。

IQueryable<string> ScannedBarcodes = XYZDb.tblScannedBarcodes
                                          .Where(i => i.PullNo == lblPullNo.Text)
                                          .Select(i => i.Barcode);

List<string> _barcodes = new List<string>();
_barcodes.AddRange(ScannedBarcodes);

There's really no need to loop over those values - just create your List<string> and then add the whole IQueryable collection to it in a single call to .AddRange() ... 确实不需要遍历这些值-只需创建List<string> ,然后在一次对.AddRange()调用.AddRange()整个IQueryable集合添加到其中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM