简体   繁体   中英

c# How to query Datatable by LINQ

I know how to query data table by LINQ but broker names coming in the columns of data table for which i am not able to query it. see how data is coming. screen shot attached.

在此处输入图像描述

see BofA Merrill Lynch or Deutsche Bank these are broker name which is coming as a column.

This way i generally query data table by LINQ.

                ds.Tables[2].AsEnumerable().Where(x => x.Field<int>("EarningID") == earningsid
                    && x.Field<string>("EarningsType") == earningsType
                    && x.Field<string>("EarningsType") == earningsType
                    && x.Field<string>("DisplayInCSM") == DisplayInCSM
                    && x.Field<string>("Type") == Type
                    && x.Field<string>("Broker") == BrokerCode
                    && x.Field<string>("Period") == Period
                    );

Now tell me how could i add one more clause in where condition that broker name.

How could i mention column name in where clause like && x.Field<string>("Deutsche Bank") as a result LINQ query should return value say 19738.5877

in my above linq query i mention few condition in where but how could i mention Deutsche Bank column name in where and i should get value 19738.5877

please share the right LINQ query where i can also mention column name in where with other condition. thanks

EDIT

I tried this way to query column but getting error Additional information: Cannot cast DBNull.Value to type 'System.Int32'. Please use a nullable type.

private string GetData<T>(DataTable table, DataTable dt, int earningsid, string earningsType, string DisplayInCSM,
    string Type, string BrokerCode, string Period, T value) where T : IComparable<T>
{
    string brokername = GetBrokerName(dt, BrokerCode);

    IEnumerable<DataRow> rows = table.Rows.OfType<DataRow>()
            .Where(r => r.Field<int>("EarningID") == earningsid
                && r.Field<string>("EarningsType") == earningsType
                && r.Field<string>("DisplayInCSM") == DisplayInCSM
                && r.Field<string>("Type") == Type
                && r.Field<string>("Broker") == BrokerCode
                && r.Field<string>("Period") == Period
                && r.Field<T>(brokername).CompareTo(value) >= 0);

    string rowValue =(rows.FirstOrDefault()[brokername]==DBNull.Value ? "" : rows.FirstOrDefault()[brokername].ToString());

    return rowValue;
}

EDIT 1

------
            IEnumerable<DataRow> rows = table.Rows.OfType<DataRow>()
                    .Where(r => r.Field<int>("EarningID") == earningsid
                        && r.Field<string>("EarningsType") == earningsType
                        && r.Field<string>("DisplayInCSM") == DisplayInCSM
                        && r.Field<string>("Type") == Type
                        && r.Field<string>("Broker") == BrokerCode
                        && r.Field<string>("Period") == Period
                        /*&& r.Field<T>(brokername).CompareTo(value) >= 0*/
                        && r.Field<object>(brokername) != DBNull.Value);

for above code i am getting this error Cannot cast DBNull.Value to type 'System.Int32'. Please use a nullable type.

You cannot compare an int with System.DbNull , that's what the exception tells you. The only int in your parameters is earningsid and indeed your screenshot shows null values for that column.

Good that the Field extension method also supports nullable types ( r.Field<int?>("...") ):

IEnumerable<DataRow> rows = table.Rows.OfType<DataRow>()
    .Where(r => r.Field<int?>("EarningID") == earningsid
        && so on ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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