locked
Linq Search Query RRS feed

  • Question

  • User-1243688316 posted
    • Hi Friends :)

    i have a datalist in my page and want to filter it by 3 dropdown!

    my query work as well when all of dropdown have selected and when one of them is empty query returns null and don't show any thing in datalist!

    But i'd like to search by every one!

    int type = int.Parse(drpType.SelectedValue);
    int contract = int.Parse(drpContract.SelectedValue);
    int location = int.Parse(drpLocation.SelectedValue);
    var select= db.View_Properties.Where(x => (x.idProType == type || x.idProType == null) && (x.idContract == contract || x.idContract == null) && (x.idLocation == location || x.idLocation == null)); DataList1.DataSource = select.ToList(); DataList1.DataBind();
    /*Contract*/ drpContract.DataTextField = "ContractName"; drpContract.DataValueField = "idContract"; drpContract.DataSource = db.tbl_Contracts.ToList(); drpContract.DataBind(); drpContract.Items.Insert(0, new ListItem("ContractName", "0")); /*Type*/ drpType.DataTextField = "TypeName"; drpType.DataValueField = "idProType"; drpType.DataSource = db.tbl_ProTypes.ToList(); drpType.DataBind(); drpType.Items.Insert(0, new ListItem("TypeName", "0")); /*Location*/ drpLocation.DataTextField = "LocationName"; drpLocation.DataValueField = "idLocation"; drpLocation.DataSource = db.tbl_Locations.ToList(); drpLocation.DataBind(); drpLocation.Items.Insert(0, new ListItem("LocationName", "0"));

    Tuesday, October 4, 2016 9:05 PM

Answers

  • User-1243688316 posted

    Thank a lot Yohann for Reply!

    I finally find the correct query to filter!

    this query work when one or two or all of dropdown were selected:

    var select = db.View_Properties.Where(x => (x.idProType == type || type==0) && (x.idContract == contract || contract==0) && (x.idLocation == location || location==0));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2016 8:23 AM

All replies

  • User36583972 posted

    Hi arfaie,

    But i'd like to search by every one!

    I'm not sure what you want to implement. May be you want to select three datasets according to the three conditions selected.

    var select1= db.View_Properties.Where(x => (x.idProType == type || x.idProType == null))
    var select2= db.View_Properties.Where(x => (x.idContract == contract || x.idContract == null))
    var select3= db.View_Properties.Where(x => (x.idLocation == location || x.idLocation == null))
    

    Feel free to let me know if I have any misunderstanding.

    Best Regards,

    Yohann Lu

    Wednesday, October 5, 2016 6:15 AM
  • User-1243688316 posted

    Thank a lot Yohann for Reply!

    I finally find the correct query to filter!

    this query work when one or two or all of dropdown were selected:

    var select = db.View_Properties.Where(x => (x.idProType == type || type==0) && (x.idContract == contract || contract==0) && (x.idLocation == location || location==0));

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 6, 2016 8:23 AM