数据表多条件选择

问题描述 投票:0回答:9

我有一个包含 4 列 A、B、C 和 D 的数据表,因此 A、B 和 C 列的值的特定组合在数据表中是唯一的。

目标: 对于给定的 A、B 和 C 列值组合,找到 D 列的值。

我想循环数据行集应该可以做到。有没有办法使用 Datatable.Select() 来完成此任务?更具体地说 - 我可以在选择过滤器中设置多个条件吗,即连接 A、B 和 C 列各条件的逻辑 AND 运算符。

c# select datatable
9个回答
140
投票

是的,

DataTable.Select
方法支持布尔运算符,就像在“真正的”SQL语句中使用它们一样:

DataRow[] results = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'");

参见 MSDN 中的DataColumn.Expression,了解 DataTable 的

Select
方法支持的语法。


47
投票

必须使用

DataTable.Select()
吗? 我更喜欢为这种事情编写 linq 查询。

var dValue=  from row in myDataTable.AsEnumerable()
             where row.Field<int>("A") == 1 
                   && row.Field<int>("B") == 2 
                   && row.Field<int>("C") == 3
             select row.Field<string>("D");

7
投票

我发现太多的 and 会返回不正确的结果(无论如何对于.NET 1.1)

DataRow[] results = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz' and D ='fred' and E = 'marg'"); 

在我的例子中,A 是表中的第 12 个字段,选择实际上忽略了它。

但是如果我这样做了

DataRow[] results = table.Select("A = 'foo' AND (B = 'bar' AND C = 'baz' and D ='fred' and E = 'marg')"); 

过滤器工作正常!


6
投票

试试这个,
我认为,这是简单的解决方案之一。

int rowIndex = table.Rows.IndexOf(table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'")[0]);
string strD= Convert.ToString(table.Rows[rowIndex]["D"]);

确保 A、B 和 C 列的值组合在数据表中是唯一的。


2
投票
    protected void FindCsv()
    {
        string strToFind = "2";

        importFolder = @"C:\Documents and Settings\gmendez\Desktop\";

        fileName = "CSVFile.csv";

        connectionString= @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+importFolder+";Extended Properties=Text;HDR=No;FMT=Delimited";
        conn = new OdbcConnection(connectionString);

        System.Data.Odbc.OdbcDataAdapter  da = new OdbcDataAdapter("select * from [" + fileName + "]", conn);
        DataTable dt = new DataTable();
        da.Fill(dt);

        dt.Columns[0].ColumnName = "id";

        DataRow[] dr = dt.Select("id=" + strToFind);

        Response.Write(dr[0][0].ToString() + dr[0][1].ToString() + dr[0][2].ToString() + dr[0][3].ToString() + dr[0][4].ToString() + dr[0][5].ToString());
    }

1
投票
Dim dr As DataRow()


dr = dt.Select("A="& a & "and B="& b & "and C=" & c,"A",DataViewRowState.CurrentRows)

其中 A、B、C 是列名称 其中第二个参数用于排序表达式


0
投票

如果您真的不想遇到很多烦人的错误(datediff 等无法在

DataTable.Select
中进行评估,即使您按照建议使用
DataTable.AsEnumerable
进行评估,您也会在评估 DateTime 字段时遇到麻烦)执行以下操作:

1)对数据建模(使用 DataTable 列创建一个类)

示例

public class Person
{
public string PersonId { get; set; }
public DateTime DateBorn { get; set; }
}

2)将此辅助类添加到您的代码中

public static class Extensions
{
/// <summary>
/// Converts datatable to list<T> dynamically
/// </summary>
/// <typeparam name="T">Class name</typeparam>
/// <param name="dataTable">data table to convert</param>
/// <returns>List<T></returns>
public static List<T> ToList<T>(this DataTable dataTable) where T : new()
{
    var dataList = new List<T>();

    //Define what attributes to be read from the class
    const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

    //Read Attribute Names and Types
    var objFieldNames = typeof(T).GetProperties(flags).Cast<PropertyInfo>().
        Select(item => new
        {
            Name = item.Name,
            Type = Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType
        }).ToList();

    //Read Datatable column names and types
    var dtlFieldNames = dataTable.Columns.Cast<DataColumn>().
        Select(item => new {
            Name = item.ColumnName,
            Type = item.DataType
        }).ToList();

    foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())
    {
        var classObj = new T();

        foreach (var dtField in dtlFieldNames)
        {
            PropertyInfo propertyInfos = classObj.GetType().GetProperty(dtField.Name);

            var field = objFieldNames.Find(x => x.Name == dtField.Name);

            if (field != null)
            {

                if (propertyInfos.PropertyType == typeof(DateTime))
                {
                    propertyInfos.SetValue
                    (classObj, ConvertToDateTime(dataRow[dtField.Name]), null);
                }
                else if (propertyInfos.PropertyType == typeof(int))
                {
                    propertyInfos.SetValue
                    (classObj, ConvertToInt(dataRow[dtField.Name]), null);
                }
                else if (propertyInfos.PropertyType == typeof(long))
                {
                    propertyInfos.SetValue
                    (classObj, ConvertToLong(dataRow[dtField.Name]), null);
                }
                else if (propertyInfos.PropertyType == typeof(decimal))
                {
                    propertyInfos.SetValue
                    (classObj, ConvertToDecimal(dataRow[dtField.Name]), null);
                }
                else if (propertyInfos.PropertyType == typeof(String))
                {
                    if (dataRow[dtField.Name].GetType() == typeof(DateTime))
                    {
                        propertyInfos.SetValue
                        (classObj, ConvertToDateString(dataRow[dtField.Name]), null);
                    }
                    else
                    {
                        propertyInfos.SetValue
                        (classObj, ConvertToString(dataRow[dtField.Name]), null);
                    }
                }
            }
        }
        dataList.Add(classObj);
    }
    return dataList;
}

private static string ConvertToDateString(object date)
{
    if (date == null)
        return string.Empty;

    return HelperFunctions.ConvertDate(Convert.ToDateTime(date));
}

private static string ConvertToString(object value)
{
    return Convert.ToString(HelperFunctions.ReturnEmptyIfNull(value));
}

private static int ConvertToInt(object value)
{
    return Convert.ToInt32(HelperFunctions.ReturnZeroIfNull(value));
}

private static long ConvertToLong(object value)
{
    return Convert.ToInt64(HelperFunctions.ReturnZeroIfNull(value));
}

private static decimal ConvertToDecimal(object value)
{
    return Convert.ToDecimal(HelperFunctions.ReturnZeroIfNull(value));
}

private static DateTime ConvertToDateTime(object date)
{
    return Convert.ToDateTime(HelperFunctions.ReturnDateTimeMinIfNull(date));
}

}
public static class HelperFunctions
{

public static object ReturnEmptyIfNull(this object value)
{
    if (value == DBNull.Value)
        return string.Empty;
    if (value == null)
        return string.Empty;
    return value;
}
public static object ReturnZeroIfNull(this object value)
{
    if (value == DBNull.Value)
        return 0;
    if (value == null)
        return 0;
    return value;
}
public static object ReturnDateTimeMinIfNull(this object value)
{
    if (value == DBNull.Value)
        return DateTime.MinValue;
    if (value == null)
        return DateTime.MinValue;
    return value;
}
/// <summary>
/// Convert DateTime to string
/// </summary>
/// <param name="datetTime"></param>
/// <param name="excludeHoursAndMinutes">if true it will execlude time from datetime string. Default is false</param>
/// <returns></returns>
public static string ConvertDate(this DateTime datetTime, bool excludeHoursAndMinutes = false)
{
    if (datetTime != DateTime.MinValue)
    {
        if (excludeHoursAndMinutes)
            return datetTime.ToString("yyyy-MM-dd");
        return datetTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
    }
    return null;
}
}

3)使用以下代码轻松将您的

DataTable
(dt)转换为对象列表:

List<Person> persons = Extensions.ToList<Person>(dt);

4) 享受使用 Linq 的乐趣,而无需使用

row.Field<type>
 时必须使用的烦人的 
AsEnumerable

示例

var personsBornOn1980 = persons.Where(x=>x.DateBorn.Year == 1980);

0
投票

试试这个:

class Program
{
    static void Main()
    {
        //  Create Your YourDataTableSample.
        //  And Add New columns and Some rows.
        DataTable YourDataTableSample = new DataTable("SampleDT");
        YourDataTableSample.Columns.Add(new DataColumn("ID", typeof(int)));
        YourDataTableSample.Columns.Add(new DataColumn("MyDate", typeof(DateTime)));
        YourDataTableSample.Rows.Add(100, new DateTime(2021, 1, 1));
        YourDataTableSample.Rows.Add(200, new DateTime(2022, 2, 1));
        YourDataTableSample.Rows.Add(300, new DateTime(2023, 3, 1));
        
        // Select by MyDate.
        DataRow[] TheResult = YourDataTableSample.Select("MyDate > #6/1/2021#");
        
        // Display The Result.
        foreach (DataRow TheRow in TheResult)
        {
            Console.WriteLine(TheRow["ID"]);
        }
    }
}

0
投票

对于那些有多个单词名称的列,即“Column A”,您可以将它们放入方括号中,如[Column A]:

DataRow[] 结果 = table.Select("[列 A] = 'foo' AND [列 B] = 'bar' AND [列 C] = 'baz');

© www.soinside.com 2019 - 2024. All rights reserved.