简体   繁体   English

MVC C# - 带条件的DataRows中的DataTable

[英]MVC C# - DataTable in DataRows with Condition

I have the following DataTable records :- 我有以下DataTable记录: -

I want to display the Rows for which HeaderPrintOrder Column don't have 0 as value. 我想要显示的Rows对于这HeaderPrintOrder列不具有0的值。 And in PDF Cell I have to print FieldName : Amount by iterating to the records with above given condition. 在PDF Cell中,我必须通过迭代到具有上述条件的记录来打印FieldNameAmount

I am trying the below code, gives me error Cannot interpret token '!' 我正在尝试下面的代码,给我错误Cannot interpret token '!' . What is correct way to do this? 这样做的正确方法是什么?

在此输入图像描述

var datatable = new DataTable();
datatable.Load(reader);

DataRow[] HeadingFields = datatable.Select("HeaderPrintOrder != 0");
                    foreach (var item in HeadingFields)
                    {
                        cellHead = new PdfPCell(new Phrase(HeadingFields[item]["FieldName"].ToString() + " : " + HeadingFields[item]["Amount"].ToString(), fntTableFont));
                        cellHead.Colspan = 3;
                        MainTable.AddCell(cellHead);
                    }

With LINQ it's easy: 使用LINQ很容易:

var filtered = datatable.AsEnumerable()
    .Where(row => row.Field<int>("HeaderPrintOrder") != 0);
foreach(DataRow row in filtered)
{
    //  ....
}

With DataTable.Select you have to use <> instead of != : 使用DataTable.Select您必须使用<>而不是!=

DataRow[] HeadingFields = datatable.Select("HeaderPrintOrder <> 0");

<> is supported whereas != is not. 支持<>!=不支持。 You can see that here: 你可以在这里看到:

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression%28v=vs.110%29.aspx

Try with Linq: 试试Linq:

var HeadingFields= from row in datatable .AsEnumerable()
                   where row.Field<int>("HeaderPrintOrder") <> (int)(0)
                   select row;

The != operator is not supported by the RowFilter syntax. RowFilter语法不支持!=运算符。

Try: 尝试:

DataRow[] HeadingFields = datatable.Select("NOT (HeaderPrintOrder = 0)");

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

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