how to get full table in to a var in c# ,mvc - c#

I have a data table and i'm filtering data from it. now I want to get full table into var like below
DataView dv = new DataView(table);
dv.RowFilter = "[CRUISE-ONLY]='YES' AND [NOW-AVAIL]='YES' AND [1A] <> 'N/A'";
table = dv.ToTable();
var allneededmsc = (from DataRow dr in table.Rows
where (string)dr["CRUISE-ONLY"] == "YES" &&
(string)dr["NOW-AVAIL"] == "YES" &&
(string)dr["1A"] != "N/A"
select (string)dr["aLL COLUMNS"]);
//here I want to select full table which fulfill the conditions.
how can I do this.

Related

How to Get Single Column Value in c# LINQ?

DataTable Master_Table = Execute_Manager.getTableDataSet(connection, select_query, master_table);
string RuleName = (From EachRow in Master_Table
where EachRow.Field<string>("RuleID") == "123456"
Select EachRow.Field<string>("RuleName")).CopyToDataTable().ToString();
I need to get single Column Value using LINQ in Datatable c#
You could do something like the following I have added in the two examples below:
string carManufacturer = "BMW";
DataTable dt = new DataTable();
int id = (from DataRow dr in dt.Rows
where (string)dr["CarManufacturer"] == carManufacturer
select (int)dr["id"]).FirstOrDefault();
string columnValue = dt.Rows[0]["ColumnName"].ToString();
What column value are you trying to fetch from your data table?

MVC C# - DataTable in DataRows with Condition

I have the following DataTable records :-
I want to display the Rows for which HeaderPrintOrder Column don't have 0 as value. And in PDF Cell I have to print FieldName : Amount by iterating to the records with above given condition.
I am trying the below code, gives me error 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:
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 !=:
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
The != operator is not supported by the RowFilter syntax.
Try:
DataRow[] HeadingFields = datatable.Select("NOT (HeaderPrintOrder = 0)");
Try with Linq:
var HeadingFields= from row in datatable .AsEnumerable()
where row.Field<int>("HeaderPrintOrder") <> (int)(0)
select row;

if condition to set the DataTextField for a databind

I'm trying to set selectively display certain text using a databind.
the code looks like this..
DataTable oDt;
oDt = Apps.GetAll();
if (oDt.Rows.Count > 0)
{
oDt.Columns.Add("AppName_ID", typeof(string), "App_Name + ' (' + App_ID + ')'");
CmbApps.DataSource = oDt;
CmbApps.DataValueField = "App_ID";
CmbApps.DataTextField = "AppName_ID";
CmbApps.DataBind();
}
The problem is that the first value shows up as: Select (0).. so I'm trying to change the datatextfield when "App_ID" = 0 so that App_ID is NOT displayed, but is in all other values.
Not sure about the syntax, but it will be close to the below.
DataTable dt2 = oDt.Select("(App_ID != 0)").CopyToDataTable();
Using Linq-To-DataTable
DataTable tblFiltered = oDt.AsEnumerable()
.Where(row => row.Field<String>("App_ID") == "1")
.CopyToDataTable();
Or you can use DataView and RowFilter
DataView dataView = oDt.DefaultView;
dataView.RowFilter = "App_ID <> 0";
UPDATE
foreach (DataRow DRow in oDt.Rows)
{
if(DRow["app_id"].ToString().Equals("0"))
DRow["AppName_ID"] = "Select";
}

How to query C# DataTable?

I have created and returned datatable, this table has 10 columns. Now i want to filter from this table based on some dynamic search parameters. How to do this? any idea would be timely help.
// This function will create and return the source table.
var DisplayTable = CreateQueryTable();
Here I want to do dynamic search like If col1=MyName and Col2=MyCity
ResultGrid.DataSource = DisplayTable;
ResultGrid.DataBind();
Panel1.Controls.Add(ResultGrid);
You can do this in these way,
1.Creating DataView Like
var dv = dataTable.DefaultView;
dv.RowFilter = "col1='MyName' and Col2='MyCity'"; // if MyName and MyCity are literal string.
or
dv.RowFilter = "col1='"+MyName+"' and Col2 ='"+ MyCity +"'";// if MyName and MyCity are string variable.
2.With DataTable Select Method, It will return array of DataRow
var rows = dataTable.Select("col1='MyName' and Col2='MyCity'"); //if string literal
or
var rows = dataTable.Select("col1='"+MyName+"' and Col2='"+MyCity+"'"); // if string variable
3.By Linq
var filterdData = from row in dataTable.AsEnumerable()
where row.Field<string>("col1") == "MyName"
&& row.Field<string>("col2") == "MyCity"
select row;
you create DataView of your datatable and use Filter
// Create a DataView
DataView dv = new DataView(yourDataTable);
dv.RowFilter = "col1='MyName' and Col2='MyCity'";
//Bind your grid with DataView
You can also use select method on your table
DataRow[] foundRows;
foundRows = yourDataTable.Select("col1='MyName' and Col2='MyCity'");
You can also use Linq To DataTable
var results = from myRow in yourDataTable.AsEnumerable()
where myRow.Field<string>("col1") == Myname &&
myRow.Field<string>("Col2") == MyCity
select myRow;

Perform Aggregate function of DataTable

I have to perform the aggregate function on the DataTable like Datatable.Compute but compute return the object i want to perform the aggregate function on the datatable and get the datarow .
_summaryTable.Compute("min(FareAdult)", whereClause
+ "AirlineDisplayName='"
+ Convert.ToString(airline["AirlineDisplayName"])
+ "' and ( Stops=0) ");
but above code will only return the min(FareAdult) but i want to select the two column based on the above condition from datatable.
How can i do it through Linq
I have to select min(FareAdult) and the TotelPrice value of same row
use Select instead compute
_summaryTable.Select("FilterationExpression");
DataRow[] dr = _summaryTable.Select("min(FareAdult),AirlineDisplayName='" + Convert.ToString(airline["AirlineDisplayName"]) + "' and ( Stops=0) ");
Here is a LINQ method. This is pseudocode since I don't know the typing of your row, and I haven't been able to test it, but the idea is the same. Use LINQ to select the rows that match your criteria, order by the FareAdult and then select the first (minimum).
var minResult = (from row in _summaryTable.Rows
where row.AirlineDisplayName == airline["AirlineDisplayName"] && row.Stops == 0
orderby row.FareAdult
select row).FirstOrDefault();
private void CalcColumns()
{
DataTable table = new DataTable ();
//enter code here
// Create the first column.
DataColumn priceColumn = new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");
priceColumn.ColumnName = "price";
priceColumn.DefaultValue = 50;
// Create the second, calculated, column.
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName = "tax";
taxColumn.Expression = "price * 0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
totalColumn.Expression = "price + tax";
// Add columns to DataTable.
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
DataRow row = table.NewRow();
table.Rows.Add(row);
DataView view = new DataView(table);
dataGrid1.DataSource = view;
}

Categories

Resources