[英]How do I iterate over an array that's currently a string using C# and add additional values in my ASP.NET Core Application?
I'm using the KendoUI in my web application.我在我的 Web 应用程序中使用 KendoUI。 One of our database tables has an array of values stored in a single column as a
varchar
.我们的一个数据库表将一组值作为
varchar
存储在单个列中。 Everyone has an opinion on this but at the moment I have to work with it.每个人对此都有自己的看法,但目前我必须接受它。
SQL Server数据库服务器
Office_Ids
1,2,3,4,5
I've been tasked with joining the office names to these ids and also making these ids nicer(styled) within the grid.我的任务是将办公室名称加入这些 ID,并使这些 ID 在网格中更好(样式化)。 The problem is that, as a string the are immutable and so it's starting to all get very complicated.
问题是,作为一个字符串是不可变的,所以它开始变得非常复杂。
The desired outcome would be the following:期望的结果如下:
"<OfficeId>1</OfficeId><OfficeName>Some Name</OfficeName><OfficeId>2</OfficeId><OfficeName>Some Name</OfficeName>"
The above outcome also assumes that I join the data to the office table.上述结果还假设我将数据加入到 office 表中。 Usually I would accomplish something like the above using regex if the data was stored in separate tables.
如果数据存储在单独的表中,通常我会使用正则表达式完成类似上面的操作。 That fact it's not is what's causing the confusion.
事实上,这不是造成混乱的原因。
Here is my controller, this is only a test controller action at the moment as I tried to work this out.这是我的控制器,这只是我尝试解决此问题时的测试控制器操作。 You'll see that I do a few things here:
你会看到我在这里做了几件事:
Call my repository to get the data调用我的存储库以获取数据
Create two lists one for the offices and another called test
for trying to add the above mentioned code items to the strings.创建两个列表,一个用于办公室,另一个称为
test
用于尝试将上述代码项添加到字符串中。
I split the array of office Ids from the Office_Ids
column and return to a list我从
Office_Ids
列中拆分 office Id 数组并返回到列表
I then iterate over that list and try to use string inserts to pop in those tags然后我遍历该列表并尝试使用字符串插入来弹出这些标签
public IQueryable Read() { IQueryable data = _company.GetAll(); public IQueryable Read() { IQueryable data = _company.GetAll();
List<string> office_item = new List<string>(); List<string> test = new List<string>(); foreach (var item in data) { office_item = item.Office_Ids .Split(',') .Select(x => x.Trim('"')) .Select(x => x.Trim(' ')).ToList(); foreach (var office in office_item ) { client.Insert(0, "<OfficeId>"); client.Insert(2, "</OfficeId>"); test.Add(client); } } Console.WriteLine(test); return data; }
The problems问题
The model for CompanyReview
looks like this: CompanyReview
的模型如下所示:
namespace MyCompany.Data
{
public class OfficeReview : BaseEntity
{
public string Office_Ids { get; set; }
}
}
I think I have covered everything here but the TL:DR of this is that I need to separate a string which is an array of values, add some opening and closing tags to it and then return it as part of my model to the grid.我想我已经涵盖了这里的所有内容,但 TL:DR 是我需要分离一个字符串,它是一个值数组,向它添加一些开始和结束标签,然后将它作为我的模型的一部分返回到网格。 After that I'll use some regex to make it look nice.
之后,我将使用一些正则表达式使其看起来不错。
Consider using XElement
.考虑使用
XElement
。 It lends itself very nicely for XML construction and manipulation using Linq queries.它非常适合使用 Linq 查询的 XML 构造和操作。
var officeItems = new List<string> { "1, 2, 42, 12313", "2, 5, 234", "1" };
var officeIdElements = officeItems
.SelectMany(s => s.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries))
.Select(id => new XElement("OfficeId", id));
//The output you requested
string officeIds = string.Concat(officeIdElements.Select(idElement => idElement.ToString()));
However, if you want to expand on this to include other elements, say OfficeName
, then you could do但是,如果您想对此进行扩展以包含其他元素,例如
OfficeName
,那么您可以这样做
var officeElements = officeIdElements.Select(idElement =>
{
var officeName = $"Office { idElement.Name }";
var officeNameElement = new XElement("OfficeName", officeName);
return new XElement("Office", idElement, officeNameElement);
});
and if you need to wrap it under a parent node for well-formed XML如果您需要将其包装在父节点下以获得格式良好的 XML
var officesElement = new XElement("Offices", officeElements);
/*
<Offices>
<Office>
<OfficeId>1</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>2</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>42</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>12313</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>2</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>5</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>234</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
<Office>
<OfficeId>1</OfficeId>
<OfficeName>Office OfficeId</OfficeName>
</Office>
</Offices>
*/
If you have to output a string, the following approach might work:如果必须输出字符串,以下方法可能有效:
var officeItems = new List<string> { "1, 2, 42, 12313", "2, 5, 234", "1" };
var officeResults = new List<string>();
foreach (var officeItem in officeItems)
{
foreach (var item in officeItem.Replace(" ", string.Empty).Split(","))
{
var officeName = service.GetOfficeName(item);
officeResults.Add($"<OfficeId>{item}</OfficeId><OfficeName>{officeName}</OfficeName>");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.