Multisorting in ASP.NET MVC 4 - c#

How do I sort on multiple columns? I tried using this expression:
if (name != null)
{
if (name.Equals(SortEnum.ASC))
{
employees = employees.OrderBy(e => e.Name);
}
else if (name.Equals(SortEnum.DESC))
{
employees = employees.OrderByDescending(e => e.Name);
}
}
if (surname != null)
{
if (surname.Equals(SortEnum.ASC))
{
employees = employees.OrderBy(e => e.Surname);
}
else if (surname.Equals(SortEnum.DESC))
{
employees = employees.OrderByDescending(e => e.Surname);
}
}
But only the last column becomes sorted. Somewhere I saw method ThenBy(), but I don't have it.
Please help.

using System.Linq.Dynamic
List<string> orderstr = new List<string>();
orderstr.Add((name != null)? name.Equals(SortEnum.ASC)? "Name ASC": "Name DESC" : string.Empty);
orderstr.Add((surname != null)? surname.Equals(SortEnum.ASC)? "Surname ASC": "Surname DESC" : string.Empty);
var reslt = employees.OrderBy(string.Join(", ",orderstr.Where(x=>!string.IsNullOrEmpty(x))));

Related

How to apply the else if condition into the LINQ query.?

I have a list of subjects 'subjectList' I want to get the value based on two conditions like if subjectValue!=null then select subjectValue else if the subject is isDefault=true then select subjectDefaultCode, I have written an individual LINQ query for both condition that but I am not getting how to apply both the condition in a single query.?
Have a look.
string subjectValueDropDown = string.Empty;
First condition.
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue != null).Select(k => k.SubjectValue).ToArray());
Second condition.
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.IsDefault == true).Select(k => k.subjectDefaultCode).ToArray());
Possbilities:
Subject1 SubjectValue=null , isDefault=false
Subject2 SubjectValue=Maths , isDefault=false
Subject3 SubjectValue=null , isDefault=true
Subject4 SubjectValue=null , isDefault=false
In this situation or another situation where any of the subject have SubjectValue!=null then I don't want the isDefault condition to get executed.
Thanks in advance.
Updated:
Output should be same as op of below query:
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue != null).Select(k => k.SubjectValue).ToArray());
if(subjectValueDropDown==""){
subjectValueDropDown = string.Join(",", subjectList.Where(x => x.IsDefault == true).Select(k => k.subjectDefaultCode).ToArray());
}
Is it possible to merge both the query into a single query to get the expected output.
Try this, it was tested in Visual Studio and values are the same as in your update:
subjectValueDropDown = string.Join(",",
subjectList
.Where(x =>
(x.SubjectValue != null)
|| (x.SubjectValue == null && x.IsDefault)
).Select(k =>
k.SubjectValue != null? k.SubjectValue : k.SubjectDefaultCode
)
.OfType<string>()
.ToArray());
using System.Collections.Generic;
using System.Linq;
namespace Game
{
class Subject
{
public string SubjectValue { get; set; }
public bool IsDefault { get; set; }
public string subjectDefaultCode = "Defauult code.";
}
class Program
{
static void Main(string[] args)
{
var subjectList = new List<Subject>
{
new Subject
{
IsDefault = false,
},
new Subject
{
SubjectValue = "Maths",
IsDefault = false,
},
new Subject
{
IsDefault = false,
},
new Subject
{
IsDefault = false,
},
};
var ubjectValueDropDown = string.Join(",", subjectList.Where(x => x.SubjectValue is not null || x.IsDefault)
.Select(x =>
{
if (x.SubjectValue is not null)
return x.SubjectValue;
if (x.IsDefault)
return x.subjectDefaultCode;
return "";
}));
}
}
}

Dynamic table name in Entity Framework

I am using Entity Framwork with a database-first approach. I want to change the table name or view name dynamically based on conditions.
Here, I am using V_OVT_VLD_340B_DNA_CLD or V_OVT_B_table or V_OVT_c_table to get the records.
Based upon the source, I need to call the different table name and get the records. The whole code snippet is the same, except for the table name.
Please refer below code
private dOVT_OutlierViewEntities db = new dOVT_OutlierViewEntities();
if(source == "a")
{
var result = this.db.V_OVT_VLD_340B_DNA_CLD.Where(x => x.DNA_PGM_PRTN_ID == partitionId && x.CLIENT_ID == clientId).ToList().Select(y => new ValidationModel
{
claim_validation_test_id = new List<byte?> { y.CLAIM_VLD_TEST_ID },
claim_id = y.CLAIM_ID,
Provider_ID = y.Provider_ID,
}).Take(id).ToList();
}
if(source == "b")
{
var result = this.db.v_OVT_B_table.Where(x => x.DNA_PGM_PRTN_ID == partitionId && x.CLIENT_ID == clientId).ToList().Select(y => new ValidationModel
{
claim_validation_test_id = new List<byte?> { y.CLAIM_VLD_TEST_ID },
claim_id = y.CLAIM_ID,
Provider_ID = y.Provider_ID,
}).Take(id).ToList();
}
if(source == "c")
{
var result = this.db.v_OVT_C_table.Where(x => x.DNA_PGM_PRTN_ID == partitionId && x.CLIENT_ID == clientId).ToList().Select(y => new ValidationModel
{
claim_validation_test_id = new List<byte?> { y.CLAIM_VLD_TEST_ID },
claim_id = y.CLAIM_ID,
Provider_ID = y.Provider_ID,
}).Take(id).ToList();
}
I want to modify the above implementation by dynamically attaching the table name to db context based upon condition.
string tableName = string.empty
if(source == "a")
tableName = "aTable";
if(source == "b")
tableName="bTable";
this.db.tableName.where().....
Is that possible?
You can go with a switch condition to set the table type and use that with context
switch (tableName)
{
case "a":
tableType = typeof(V_OVT_VLD_340B_DNA_CLD);
break;
case "b":
tableType = typeof(v_OVT_B_table);
break;
default:
tableType = typeof(v_OVT_C_table);
break;
}
var query = context.Set(tableType);
var result = query.Find(); //filter with this query condition
You can do something like this..
string tableName = string.empty
if(source == "a")
tableName =db.GetTable("aTable");
if(source == "b")
tableName=db.GetTable("bTable");
and then query like..
tableName.where()

How to Filter linq query

I am able to filter the data with the following two parameters id1 and id2, and get accurate result of 10 records, from which have 9 with a price_type=cs and other with price-type=ms.
However, if I add price_type to the parameters id1 and id2 (id1=23456,567890&id2=6782345&price_type=ms), I get 3000 records instead of getting one record.
Am I missing something in the code. Any help would be very much appreciated.
var data = db.database_BWICs.AsQueryable();
var filteredData = new List<IQueryable<database_Data>>();
if (!string.IsNullOrEmpty(query.name))
{
var ids = query.name.Split(',');
foreach (string i in ids)
{
filteredData.Add(data.Where(c => c.Name != null && c.Name.Contains(i)));
}
}
if (!string.IsNullOrEmpty(query.id2))
{
var ids = query.id2.Split(',');
foreach (string i in ids)
{
filteredData.Add(data.Where(c => c.ID2!= null && c.ID2.Contains(i)));
}
}
if (!string.IsNullOrEmpty(query.id1))
{
var ids = query.id1.Split(',');
foreach (string i in ids)
{
filteredData.Add(data.Where(c => c.ID1!= null && c.ID1.Contains(i)));
}
}
if (query.price_type != null)
{
var ids = query.price_type.Split(',');
foreach (string i in ids)
{
filteredData.Add(data.Where(c => c.Type.Contains(i)));
}
}
if (filteredData.Count != 0)
{
data = filteredData.Aggregate(Queryable.Union);
}
Updated Code:
var data = db.database_BWICs.AsQueryable();
if (!string.IsNullOrEmpty(query.name))
{
var ids = query.name.Split(',');
data = data.Where(c => c.Name != null && ids.Contains(c.Name));
}
if (query.price_type != null)
{
var ids = query.price_type.Split(',');
data = data.Where(c => ids.Contains(c.Cover));
}
if (!String.IsNullOrEmpty(query.id1))
{
var ids = query.id1.Split(',');
data = data.Where(c => c.ID1!= null && ids.Contains(c.ID1));
}
Because you don't add filter to restrict, every filter adds datas to result.
It means you make OR between your filters, not AND.
And your usage of contains looks rather strange too : you're using String.Contains, while I would guess (maybe wrong) that you want to see if a value is in a list => Enumerable.Contains
You should rather go for something like this (withoud filteredData)
var data = db.database_BWICs.AsQueryable();
if (!string.IsNullOrEmpty(query.name))
{
var ids = query.name.Split(',');
data = data.Where(c => c.Name != null && ids.Contains(c.Name)));
}
//etc.
if (query.price_type != null)
{
var ids = query.price_type.Split(',');
data = data.Where(c => ids.Contains(c.Type));
}
EDIT
Well, if you wanna mix and or conditions, you could go for PredicateBuilder
Then your code should look like that (to be tested).
//manage the queries with OR clause first
var innerOr = Predicate.True<database_BWICs>();//or the real type of your entity
if (!String.IsNullOrEmpty(query.id1))
{
var ids = query.id1.Split(',');
innerOr = innerOr.Or(c => c.ID1!= null && ids.Contains(c.ID1));
}
if (!String.IsNullOrEmpty(query.id2))
{
var ids = query.id2.Split(',');
innerOr = innerOr.Or(c => c.ID2!= null && ids.Contains(c.ID2));
}
//now manage the queries with AND clauses
var innerAnd = Predicate.True<database_BWICs>();//or the real type of your entity
if (query.price_type != null)
{
var ids = query.price_type.Split(',');
innerAnd = innerAnd.And(c => ids.Contains(c.Type));
}
//etc.
innerAnd = innerAnd.And(innerOr);
var data = db.database_BWICs.AsQueryable().Where(innerAnd);

How can i cast IQueryable<> query to IQueryable<obj.getType()>?

How can i factorize this code?
my filter returns several type of document. the difficult is that i have some query per type... i would like a generic method to return a correct query
thanks
if(this.comboBoxType.Text.Equals("Vente"))
{
IQueryable<Vente> queryV = ContexteDAO.ContexteDonnees.Vente
.Include("Client")
.Include("Paiement")
.Include("Employe").OrderBy(v => v.venteID);
if (this.tbxNomCli.Text != "")
queryV = queryV.Where(v => v.Client.nom.Contains(this.tbxNomCli.Text));
if (this.comboBoxEtat.Text != "Tous")
queryV = queryV.Where(v => v.etat == this.comboBoxEtat.Text);
if (this.checkBoxDate.Checked)
queryV = queryV.Where(v => v.date.Equals(this.dateTimePicker.Value.Date));
if (this.tbxTva.Text != "")
queryV = queryV.Where(v => v.Client.numEntreprise.Contains(this.tbxTva.Text));
if (this.checkBoxVendeur.Checked)
{
Employe employe = this.comboBoxVendeur.SelectedItem as Employe;
queryV = queryV.Where(v => v.Employe.login.Equals(employe.login));
}
this.documentBindingSource.DataSource = queryV.ToList();
}
if (this.comboBoxType.Text.Equals("Commande"))
{
IQueryable<Commande> queryC = ContexteDAO.ContexteDonnees.Commande
.Include("Client")
.Include("Paiement")
.Include("Employe").OrderBy(c => c.commandeID);
if (this.tbxNomCli.Text != "")
queryC = queryC.Where(v => v.Client.nom.Contains(this.tbxNomCli.Text));
if (this.comboBoxEtat.Text != "Tous")
queryC = queryC.Where(v => v.etat == this.comboBoxEtat.Text);
if (this.checkBoxDate.Checked)
queryC = queryC.Where(v => v.date.Equals(this.dateTimePicker.Value.Date));
if (this.tbxTva.Text != "")
queryC = queryC.Where(v => v.Client.numEntreprise.Contains(this.tbxTva.Text));
if (this.checkBoxVendeur.Checked)
{
Employe employe = this.comboBoxVendeur.SelectedItem as Employe;
queryC = queryC.Where(v => v.Employe.login.Equals(employe.login));
}
this.documentBindingSource.DataSource = queryC.ToList();
}
You could make a generic approach:
public IQueryable<T> GetData<T>( string identifier )
{
switch( identifier )
{
case "Vente":
{
return ContexteDAO.ContexteDonnees.Vente
.Include("Client")
.Include("Paiement")
.Include("Employe")
.OrderBy(v => v.venteID);
// do more stuff
break;
}
// add more cases
default:
{
return null;
}
}
}
the call would look like:
IQueryable<Vente> result = GetData<Vente>( "Vente" );
it would solve your problem but i won't like it, because you need to specify the type AND need an identifier which selection you would like to perform. This could lead to an exception really fast when you have something like GetData<Vente>( "OtherEntity" ).

c# Ordering Variable data using a drop down list

I am currently stuck on a small problem.
I have a List View which displays products and I am looking to include a drop down list, with a filter button which when pressed will order the products by high-low price or low-high.
Here is my code.
protected void LB_Filter_Click(object sender, EventArgs e)
{
using (DataClasses_ECDataContext db = new DataClasses_ECDataContext())
{
DT_Product Pro = db.DT_Products.SingleOrDefault(x => x.ProductID == int.Parse(ViewState["ProductID"].ToString()));
var product = from x in db.DT_Products
where x.RangeID == Pro.RangeID
select new
{
x.ProductName,
x.ProductID,
x.Sale_Price,
Link = RouteTable.Routes.GetVirtualPath(null, "Product-by-tag", codesnippets.RouteLink(x.ProductID, x.ProductName, 'p')).VirtualPath,
};
if (DDL_Order.SelectedIndex == 0)
{
product.OrderByDescending(v => v.Sale_Price);
}
else if (DDL_Order.SelectedIndex == 1)
{
product.OrderBy(v => v.Sale_Price);
}
LV_Products.DataSource = product;
LV_Products.DataBind();
}
}
Any Help would be fantastic,
Thanks!
You need to execute the results by converting it to list to get the expected output, like this:
List<DT_Product> products = new List<DT_Product>();
if (DDL_Order.SelectedIndex == 0) {
products = product.OrderByDescending(v => v.Sale_Price).ToList();
} else if (DDL_Order.SelectedIndex == 1) {
products = product.OrderBy(v => v.Sale_Price).ToList();
}
LV_Products.DataSource = products;
LV_Product.DataTextField = "ProductName";
LV_Product.DataValueField = "ProductID";
LV_Products.DataBind();
Are you missing this?
LV_Products.DataSource = product;
LV_Product.DataTextField="ProductName";
LV_Product.DataValueField="ProductID";
LV_Products.DataBind();

Categories

Resources