简体   繁体   English

具有多个可能的列名称的DataTable

[英]DataTable with multiple possible names for Column

Importing data from a Datatable and a column name could have multiple name values, eg. 从数据表和列名称导入数据可以具有多个名称值,例如。 the column might be called "Name" or it could be called "First Name" or "F name" 该列可能称为“名称”,也可能称为“名字”或“ F名称”

is there a more efficient way then doing lots of If else's to assign the value of a column 有没有一种更有效的方法然后进行大量的If If来分配列的值

What I have at the moment is 我目前所拥有的是

   foreach (DataRow item in datatable.Rows)
            {
                var csvEmployee = new CsvEmployee();

                if(datatable.Columns.Contains("Name"))
                    csvEmployee.FirstName = item["Name"].ToString();
                else if (datatable.Columns.Contains("First Name"))
                    csvEmployee.FirstName = item["First Name"].ToString();
                else if (datatable.Columns.Contains("F Name"))
                    csvEmployee.FirstName = item["F Name"].ToString();
            }

Check before the FOR statement what your column is called (by Columns.Contains), assign it to variable and use it in a loop. 在FOR语句之前检查(由Columns.Contains调用)您的列,将其分配给变量并在循环中使用它。 btw: Try to not use ToString method. btw:尝试不使用ToString方法。

        string columnName = null;
        if (datatable.Columns.Contains("Name"))
            columnName = "Name";
        else if (datatable.Columns.Contains("First Name"))
            columnName = "First Name";
        else if (datatable.Columns.Contains("F Name"))
            columnName = "F Name";
       if (columnName == null)
            return;
        foreach (DataRow item in datatable.Rows)
        {
            var csvEmployee = new CsvEmployee();
            csvEmployee.FirstName = item[columnName].ToString();
        }
string[] firstnames = new String[] { "Name", "First Name", "F Name" } //Add more if u want

foreach (string x in firstnames)
    {
        if (datatable.Columns.Contains(x)) csvEmployee.FirstName = item[x];
    }

You can replace the if / else if with the ternary operator although readability is not the best. 尽管可读性不是最好的,但是您可以用三元运算符替换if / else if

do this before the loop: 在循环之前执行此操作:

var columns = datatable.Columns;
var columnName = columns.Contains("Name") ? "Name" 
                  : columns.Contains("First Name") ? "First Name" 
                          : columns.Contains("F Name") ? "F Name" : null;
if (columnName == null) return; // assuming the containing method returns void

Then within the loop, simply do: 然后在循环中,只需执行以下操作:

csvEmployee.FirstName = item[columnName].ToString();

If the containing method doesn't return void then don't do if (columnName == null) return; 如果containing方法没有返回void那么if (columnName == null) return;则不要执行if (columnName == null) return; as suggested above but rather you can perform a null check that only executes the loop when columnName is not null . 如上所述,但是您可以执行null检查,该检查仅在columnName不为null时执行循环。

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

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