简体   繁体   English

如何使用 C# 迭代当前为字符串的数组并在我的 ASP.NET Core 应用程序中添加其他值?

[英]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问题

  • This doesn't really work very well这真的不太好用
  • I'll never know how long an Office Id is, and so if they are ever 4 numbers long, this insert approach will not work.我永远不会知道 Office Id 有多长,因此如果它们的长度为 4 个数字,则此插入方法将不起作用。
  • I need to return those values to my grid as part of the model which it's not doing at the moment and finally我需要将这些值作为模型的一部分返回到我的网格中,它目前没有这样做,最后
  • I still need to join the office table to these Ids.我仍然需要将办公桌加入这些 ID。

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.

相关问题 在 C# ASP.NET Core 3.1 应用程序中,我如何路由到 controller 然后查询字符串? - In a C# ASP.NET Core 3.1 application how do I route to a controller then query string? 如何从我的 ASP.NET 核心应用程序访问来自 Azure Active Directory 的其他数据? - How do I access additional data from Azure Active Directory from my ASP.NET Core application? 如何获取 Asp.Net Core DropdownList 以在 C# 枚举中显示 [Display()] 值? - How do I get an Asp.Net Core DropdownList to show the [Display()] values in a C# enum? 如何将控制台C#应用程序添加到ASP.NET WebSite? - How do I add a console C# application to a ASP.NET WebSite? 如何使用 ASP.NET Core MVC c# 生成 PDF 文档 - How do I a generate PDF document using ASP.NET Core MVC c# 如何在应用程序状态(C#ASP.net MVC)中存储列表〜数组 - How do I store List ~ Array in the Application State (C# ASP.net MVC) 如何使用asp.net将值从C#发送到javascript - How do I send my values from c# to javascript with asp.net ASP.NET 核心 - 如何从我的 C# class 到我的 Z558B5744CF685F39D34EZ906B 客户端应用程序中获取我的状态代码? - ASP.NET Core - How do I get my status code from my C# class to my TypeScript Client App? 我如何在c#asp.net应用程序中使用jquery实现文本框自动建议? - How can i implement textbox auto suggest using jquery in my c# asp.net application? 如何创建自己的验证码并使用c#在asp.net中进行验证? - How do I create my own captcha and verify it in asp.net using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM