在C#中获取第一列和最后一列值 [英] Get first and last column value in C#

查看:302
本文介绍了在C#中获取第一列和最后一列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET网络服务中使用以下代码,可以通过CSV文件获取其数据。

I am using the following code in my .NET web service that gets its data form a CSV file.

private List<Item> ietms = new List<Item>();

public ItemRepository()
{
    string filename = HttpRuntime.AppDomainAppPath + "App_Data\\items.csv";

    var lines = File.ReadAllLines(filename).Skip(1).ToList();

    for (int i = 0; i < lines.Count; i++)
    {
        var line = lines[i];

        var columns = line.Split('$');

        //get rid of newline characters in the middle of data lines
        while (columns.Length < 9)
        {
            i += 1;
            line = line.Replace("\n", " ") + lines[i];
            columns = line.Split('$');
        }

        //Remove Starting and Trailing open quotes from fields
        columns = columns.Select(c => { if (string.IsNullOrEmpty(c) == false) { return c.Substring(1, c.Length - 2); } return string.Empty; }).ToArray();

        var temp = columns[5].Split('|', '>');

        items.Add(new Item()
        {
            Id = int.Parse(columns[0]),
            Name = columns[1],
            Description = columns[2], 
            Category = temp[0]
        });
    }
}

此代码从CSV文件中获取产品列表以及其名称,描述等。每个产品属于一个或两个类别: Category = temp [0]

This code gets a list of products from the CSV file along with its name, description etc. Each product belongs to either one or two categories : Category = temp[0].

每个产品的类别都位于csv文件的一列,其数据结构如下:

Each product's category is found in a column of the csv file with it's data structured as such:

组>子组> code>,在这种情况下,该产品属于类别组。

Groups>Subgroup>item, in which case, this product belongs to category "Groups".

产品的类别列也可以结构为:

The category column of a product may also be structured as:

MajorGroup | Groups> Subgroup> item ,在这种情况下,此产品属于类别MajorGroup。

MajorGroup|Groups>Subgroup>item, in which case this product belongs to category "MajorGroup".

此外,在许多情况下,产品的类别列可以被构造为:

Also, in many cases a product's category column may be structured as:

MajorGroup | Groups> Subgroup> item | SecondGroup ,在这种情况下,此产品属于类别MajorGroup和SecondGroup

MajorGroup|Groups>Subgroup>item|SecondGroup, in which case this product belong to both the categories "MajorGroup" and "SecondGroup"

使用做一半的工作。如果产品在CSV文件中定义为 MajorGroup | Groups> Subgroup> item | SecondGroup ,则将其分配给类别MajorGroups,而不是SecondGroup。

The code above that I am currently using does half the job. If a product has a category defined in the CSV file as MajorGroup|Groups>Subgroup>item|SecondGroup, it assigns it to category "MajorGroups" but not "SecondGroup".

此行 var temp = columns [5] .Split('|','>'); 获得第一个值结构化的taht方式,并用管道分隔,并将其设置为产品的类别 Category = temp [0]

This line var temp = columns[5].Split('|', '>'); gets the first value structured taht way and separated by a pipe and sets it as the product's category here Category = temp[0].

如何解决这个问题,如果类别的结构为 MajorGroup | Groups> Subgroup> item | SecondGroup

How do I fix this so that if the category is structured as MajorGroup|Groups>Subgroup>item|SecondGroup, with two categories, then it will show up in both categories.

如何根据类别列数据的结构将产品分配到一个或多个类别。

How do I assign the product to one or more categories depending on the structure of the category column data.

这大部分工作,但是如何修改代码来检查和分配这两个类别?

This works for the most part, but how do I alter the code to check and assign for both categories?

我可以更改 var temp = columns [5] .Split('|','>'); 来获取第一个和最后一个值> Category = temp [0]

Can I change this var temp = columns[5].Split('|', '>'); to get both teh first and the last value if it exists and assign both to Category = temp[0].

推荐答案

可以执行以下操作。

    ...
    var temp = columns[5].Split('|', '>');
    string categories= temp[0];
    if (input.Count(x => x == '|') >= 2)
    {
        categories+= "," + temp.Last();
    }
    ...
    Category = categories;

然后,可以通过以下函数获取分配给类别的Items列表:

Then one could get a list of Items that is assigned to a category by the following function:

    static public IList<Item> GetProductsByCategory(string category, IList<Item> items)
    {
        return items.Where(x => x.Category.Split(',').Contains(category,StringComparer.OrdinalIgnoreCase)).ToList();
    }

一个更干净的解决方案是将Item类中的类别存储为实施 ILIST

A much cleaner solution is to store the categories within the Item class as something that implements ILIST.

这篇关于在C#中获取第一列和最后一列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆