简体   繁体   English

在LINQ中排除该列中所有行均为null或等于0的列

[英]Exclude columns where all rows in that column are null or equal to 0 in LINQ

I need to exclude a column from a DataSet Query where that column only contains 0's (Nulls). 我需要从DataSet查询中排除一列,其中该列仅包含0(空)。

All solutions I have found (for example: Filtering Null values in Select ) only process the condition per row and won't take into to account all the values in the column as I need. 我发现的所有解决方案(例如: 在Select中过滤Null值 )仅处理每行的条件,而不会根据需要考虑列中的所有值。

The query needs to be generic to be used across multiple different data tables so I cannot explicitly state column names to exclude. 该查询需要通用才能在多个不同的数据表中使用,因此我无法明确说明要排除的列名。

The latest code I've tried is: 我尝试过的最新代码是:

theTable = result.Tables[0];
var query = theTable.AsEnumerable().Select(r => r.ItemArray.Where(c => long.Parse(c.ToString()) != 0));

This excludes all 0's in the row, but it does not maintain the column structure and I end up with rows with different Lengths (column sizes). 这排除了行中的所有0,但是它不维持列结构,因此我最终得到具有不同长度(列大小)的行。

This is my example SQL for reference: 这是我的示例SQL供参考:

SELECT t1.TableIndex
, CASE WHEN t1.EntityName <> t2.EntityName THEN 1 ELSE 0 END AS EntName
, CASE WHEN t1.EntityNumber <> t2.EntityNumber THEN 1 ELSE 0 END AS EntNumber

FROM DbEnv.dbo.tblOne t1 (NOLOCK)
INNER JOIN DbEnv.dbo.tblTwo t2 (nolock) ON t1.TableIndex = t2.TableIndex

WHERE t1.EntityName <> t2.EntityName
OR t1.EntityNumber <> t2.EntityNumber

Example Data Set (In this scenario, only Col2 should be excluded): 示例数据集(在这种情况下,应仅排除Col2):

     Col1 | Col2 | Col3
Row1: 0      0      1
Row2: 1      0      1
Row3: 0      0      0

Example Data Set 2 (In this scenario, Col1 and Col4 should be excluded): 示例数据集2(在这种情况下,应排除Col1和Col4):

     Col1 | Col2 | Col3 | Col4 | Col5
Row1: 0      0      1      0      1
Row2: 0      0      1      0      1
Row3: 0      1      0      0      1
Row3: 0      1      0      0      1

(Solution can be in SQL or LINQ, but I would think it would be cleaner to have the solution in LINQ) (解决方案可以使用SQL或LINQ,但我认为在LINQ中使用该解决方案会更干净)

This can be done in three queries - one running against your RDBMS, and two in-memory queries: 这可以通过三个查询来完成-一个针对您的RDBMS运行,以及两个内存中查询:

  • First query would read all columns, and bring them into memory 第一个查询将读取所有列,并将它们带入内存
  • Second query would determine which columns to keep 第二个查询将确定保留哪些列
  • Third query would project out the columns that you do not need 第三个查询将投影出您不需要的列

First query would be a "plain" query for all columns: 第一个查询将是对所有列的“普通”查询:

var allRows = theTable.ToList();

The second query could go like this: 第二个查询可能如下所示:

var columnsToKeep = Enumerable
    .Range(0, columnCount)
    .Where(i => allRows.Any(r => r.ItemArray[i] != null && long.Parse(r.ItemArray[i].ToString()) != 0 ))
    .ToList();

The third query would be like this: 第三个查询是这样的:

var query = allRows.Select(r =>
    columnsToKeep.Select(i => r.ItemArray[i]).ToArray()
);

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

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