简体   繁体   English

Linq不包括列

[英]Linq Excluding a column

All -- I am having a small problem trying to exclude a column from a Linq query. 全部-我在尝试从Linq查询中排除列时遇到了一个小问题。 What I am trying to to do is to find all rows where at least one of the columns with the exception of the ID column have data. 我想做的是查找所有行,其中至少有一个列(ID列除外)具有数据。 The ID column is set to Auto increment so it will always have data. ID列设置为“自动递增”,因此它将始终有数据。 Typically, the Data Table is loaded from some other data store (ie Excel, Access, CSV, etc) and sometimes a few of these rows contain no data. 通常,数据表是从其他一些数据存储区(例如Excel,Access,CSV等)加载的,有时其中一些行不包含任何数据。 My current work around is to drop that ID Column from the Data Table (dt1) and then add it back to the data table and re-populate the column. 我当前的解决方法是从数据表(dt1)中删除该ID列,然后将其重新添加到数据表中,然后重新填充该列。 Note: The total number of columns can vary from as little as 10 to as many as 100 so any solution has to be dynamic. 注意:列的总数可以从最少10个到最多100个不等,因此任何解决方案都必须是动态的。

In my example below only rows 1 through 4 contain data in all columns where Rows 0 and 5 only contain data in the ID Column. 在下面的示例中,只有第1行到第4行包含所有列中的数据,而第0行和第5行仅包含ID列中的数据。 In dt2 I only want Rows 1 through 4 added. 在dt2中,我只希望添加1至4行。 So My Question is how would I go about removing the ID Column from my LINQ query below? 因此,我的问题是如何从下面的LINQ查询中删除ID列?

Thanks for any constructive suggestions in advance! 感谢您提前提出任何建设性建议!

private void LoadDataTable()
{
        DataTable dt1 = new DataTable();

        //Create three columns
            DataColumn dc = new DataColumn("ID", typeof(Int32));
            dc.AutoIncrement = true;
            dc.AllowDBNull = false;
            dc.AutoIncrementSeed = 1;
            dt1.Columns.Add(dc);
            dc.SetOrdinal(0);

            dc = new DataColumn("Item", typeof(String));
            dt1.Columns.Add(dc);

            dc = new DataColumn("Quantity", typeof(Int32));
            dt1.Columns.Add(dc);

        //Create items Array
            string[] items = new string[] { "Ford", "Chevy", "GMC", "Buick", "Toyota", "Honda", "Mercury"
                                                ,"Hyundai", "Rolls Royce", "BMW", "Mercedes Benz", "Bentley", "Porche"
                                                ,"Infinity", "Jaguar" };

        //Load Dummy data
            Random rnd = new Random();
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dt1.NewRow();

                if (i > 0 && i < 4)
                {
                    dr[1] = items[rnd.Next(i, items.Count())];
                    dr[2] = rnd.Next(i, 10000);

                    //Remove previously used Item from Array
                        items = items.Where(w => w != dr[1].ToString()).ToArray(); 
                }
                dt1.Rows.Add(dr);
            }

        //dt2 should only contain rows who have data in at least one column with the exception of the ID Column
            DataTable dt2 = dt1.Rows.Cast<DataRow>()
                            .Where(w => w.ItemArray.Any(field => !string.IsNullOrEmpty(field.ToString().Trim()) && !(field is DBNull)))
                            .CopyToDataTable();
}

Update 更新资料

Here is the fix thanks to Ivan Stoev 这是对Ivan Stoev的修复

    //dt2 should only contain rows who have data in at least one column with the exception of the ID Column
    var columns = dt1.Columns.Cast<DataColumn>().Where(c => c.ColumnName != "ID").ToList();

    DataTable dt2 = dt1.Rows.Cast<DataRow>()
                    .Where(w => columns.Select(c => w[c]).Any(field => !string.IsNullOrEmpty(field.ToString().Trim()) && !(field is DBNull)))
                    .CopyToDataTable();

You need to replace the ItemArray inside the query with some alternative DataRow value access method, for instance like this 您需要使用一些替代的DataRow值访问方法替换查询中的ItemArray ,例如这样

var columns = dt1.Columns.Cast<DataColumn>().Where(c => c.ColumnName != "ID").ToList();
DataTable dt2 = dt1.Rows.Cast<DataRow>()
    .Where(r => columns.Select(c => r[c]).Any(field => ... 

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

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