Skip to content

Instantly share code, notes, and snippets.

@ritacse
Last active August 17, 2022 07:26
Show Gist options
  • Save ritacse/27100f331f7048971189f8732e6f50e8 to your computer and use it in GitHub Desktop.
Save ritacse/27100f331f7048971189f8732e6f50e8 to your computer and use it in GitHub Desktop.
///----Order By Descending & then OrderBy Ascending (with 2 columns)
DataTable dtOrderTemp = DTAllPODetails.AsEnumerable().Where(i => i.Field<String>("Order Code").Contains(
dtOrderCollection.Rows[ordercount][0].ToString().Trim())).OrderByDescending(i => i.Field<String>("GroupCode")).ThenBy(i => i.Field<int>("SerialNo")).CopyToDataTable();
var distinctPOValues = excelDataTable.AsEnumerable().Select(row => new
{
DistinctPO = row.Field<string>("PO Code"),
DistinctOrderNo = row.Field<string>("Order NO")
}).Distinct().ToList();
// Distinct with where condition
for (int i = 0; i < distinctPOValues.Count; i++)
{
var DistinctOrder = excelDataTable.AsEnumerable().Where(r => r.Field<string>("Buyer PO") == distinctPOValues[i].DistinctPO.ToString()
).Select(row => new
{
DistinctOrderCode = row.Field<string>("Order Code"),
DistinctOrderNo = row.Field<string>("Order NO")
}).Distinct().ToList();
}
/// Get cell value from dataTable with where condition.
decimal cst08 = dtOthers.AsEnumerable().First(x => x.Field<string>("Cost For") == "00008").Field<decimal>("Total Cost");
decimal cst49 = dtOthers.AsEnumerable().First(x => iManipulator.GetCode(x.Field<string>("Cost For")) == "00049").Field<decimal>("Total Cost");
string headName = dtCstHead.AsEnumerable().First(x => x.Field<string>("code") == "00049").Field<string>("name") + "(00049)";
MessageBox.Show("Provided cost seem not valid. Please provide valid cost for " + headName + " on Others Tab");
// get value from dataTable
var price = orderInfoDataTable.AsEnumerable().Where(p => p.Field<string>("Product Code") == cmbBin.Text.Trim()).Select(p => p.Field<decimal>("Price")).FirstOrDefault();
decimal cstSum = dtOthers.AsEnumerable().Where(c => iManipulator.GetCode(c.Field<string>("Cost For")) == "00021"
|| iManipulator.GetCode(c.Field<string>("Cost For")) == "00038"
iManipulator.GetCode(c.Field<string>("Cost For")) == "00046"
|| iManipulator.GetCode(c.Field<string>("Cost For")) == "00053").Sum(c => c.Field<decimal>("Total Cost"));
/*
1. You can't query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. You need to use the AsEnumerable() extension for DataTable
2. AsEnumerable() returns IEnumerable<DataRow>. If you need to convert IEnumerable<DataRow> to a DataTable, use the CopyToDataTable() extension.
*/
EnumerableRowCollection<DataRow> lcCombDataSource;
EnumerableRowCollection<DataRow> scCombDataSource;
DataTable dtLcScTempList = new DataTable();
if (!ChkAllLcSc.Checked)
dtLcScTempList = dtLcScList.AsEnumerable().Where(c => c.Field<string>("Companycode") == iManipulator.GetCode(cmbCompany.Text.Trim())
&& c.Field<string>("Buyer_code") == iManipulator.GetCode(cmbByerName.Text)).CopyToDataTable();
else
dtLcScTempList = dtLcScList;
if (lcScId == 0)
{
lcCombDataSource = dtLcScList.AsEnumerable().Where(c => c.Field<int>("Type") == (int)LcScType.Lc);
scCombDataSource = dtLcScList.AsEnumerable().Where(c => c.Field<int>("Type") == (int)LcScType.Sc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment