LINQ and dynamic where clauses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWlrZSBDb2xsaW5z?=

    LINQ and dynamic where clauses

    I am trying to set up a dynamic search using linq. I believe the syntax is
    correct, but cannot confirm it because when I try to cast my
    Session[“Employees”] from a List<to IQueryable<>, I get a cast error
    “Unable to cast object of type System.Collecti ons.Generic.Lis t to type
    System.Linq.IQu eryable”. Is there a way to cast List<to IQueryable<>, or is
    there a different way I need to be doing this?

    protected void btnSearch_Click (object sender, EventArgs e)
    {
    try
    {
    if (Session["Employees"] != null)
    {
    IQueryable<Busi nessLogic.Servi ces.UserProfile employees =
    (IQueryable<Bus inessLogic.Serv ices.UserProfil e>)(List<Busine ssLogic.Service s.UserProfile>) Session["Employees"];

    switch (ddSearchCriter ia.SelectedValu e)
    {
    case "UserName":
    employees.Where (c =c.Username ==
    txtSearchCriter ia.Text);
    break;

    case "Email":
    employees.Where (c =c.EmailAddress ==
    txtSearchCriter ia.Text);
    break;

    default: //LastName
    employees.Where (c =c.Person.LastN ame ==
    txtSearchCriter ia.Text);
    break;
    }

    BindGrid(employ ees.Select(c =>
    c).ToList<Busin essLogic.Servic es.UserProfile> ());
    }
    else
    {
    List<BusinessLo gic.Services.Us erProfileemploy ees = null;
    Shared.Business Logic.UserSearc hFilter filter = new
    UserSearchFilte r();

    switch (ddSearchCriter ia.SelectedValu e)
    {
    case "UserName":
    filter.Username = txtSearchCriter ia.Text;
    break;

    case "Email":
    filter.EmailAdd ress = txtSearchCriter ia.Text;
    break;

    default: //LastName
    filter.LastName = txtSearchCriter ia.Text;
    break;
    }

    PagedResult<Bus inessLogic.Serv ices.UserProfil epagedResult =
    SearchEmployees (filter);
    employees =
    pagedResult.Res ults.ToList<Bus inessLogic.Serv ices.UserProfil e>();
    BindGrid(employ ees.ToList<Busi nessLogic.Servi ces.UserProfile >());
    }
    }
    catch (ThreadAbortExc eption) { }
    catch (Exception ex) { ExceptionHelper .Publish(ex); }
    }

  • mesut

    #2
    Re: LINQ and dynamic where clauses

    Hi Mike, there are several ways of doing this. But I got this way
    working...

    I hope it helps,

    cheers, mesut


    objDataContext = DatabaseFactory .GetLinq();
    List<Linq.tblCo stLocalTable;
    var query = (from costing in objDataContext. tblCosts
    select costing);

    //the where code below (2 lines) are
    replace by dynamic where
    //where costing.Product ID == productID &&
    //costing.Busines sUnit == businessUnit

    if (productID != 0) query = query.Where(cos t =>
    cost.ProductID == productID);
    if (!string.IsNull OrEmpty(busines sUnit)) query =
    query.Where(cos t =cost.BusinessU nit == businessUnit);

    LocalTable = query.ToList();

    return LocalTable;

    Comment

    • =?Utf-8?B?TWlrZSBDb2xsaW5z?=

      #3
      Re: LINQ and dynamic where clauses

      Thanks for the reply, but I am not able to get it to work yet. Can you, or
      anyone, offer some input.

      Below is what I tried...also I decided that I needed to use SQLMethods.Like

      List<.BusinessL ogic.Services.U serProfileemplo yees =
      (List<.Business Logic.Services. UserProfile>)Se ssion["Employees"];
      var query = from p in employees
      select p;

      switch (ddSearchCriter ia.SelectedValu e)
      {
      case "UserName":
      query.Where(c =SqlMethods.Lik e(c.Username, "%" + txtSearchCriter ia.Text
      + "%"));
      break;

      case "Email":
      query.Where(c =SqlMethods.Lik e(c.EmailAddres s, "%" +
      txtSearchCriter ia.Text + "%"));
      break;

      default: //LastName
      query.Where(c =SqlMethods.Lik e(c.Person.Last Name, "%" +
      txtSearchCriter ia.Text + "%"));
      break;
      }

      employees = query.ToList();

      BindGrid(employ ees);


      "mesut" wrote:
      Hi Mike, there are several ways of doing this. But I got this way
      working...
      >
      I hope it helps,
      >
      cheers, mesut
      >
      >
      objDataContext = DatabaseFactory .GetLinq();
      List<Linq.tblCo stLocalTable;
      var query = (from costing in objDataContext. tblCosts
      select costing);
      >
      //the where code below (2 lines) are
      replace by dynamic where
      //where costing.Product ID == productID &&
      //costing.Busines sUnit == businessUnit
      >
      if (productID != 0) query = query.Where(cos t =>
      cost.ProductID == productID);
      if (!string.IsNull OrEmpty(busines sUnit)) query =
      query.Where(cos t =cost.BusinessU nit == businessUnit);
      >
      LocalTable = query.ToList();
      >
      return LocalTable;
      >

      Comment

      • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

        #4
        RE: LINQ and dynamic where clauses

        the where clause in a Linq query does not set a property, its a method
        (actually an extension method) that returns another IQueryable object that
        the Select method (which also return an IQueryable) can be called on. your
        code is throwing away the results of the Where.




        -- bruce (sqlwork.com)


        "Mike Collins" wrote:
        I am trying to set up a dynamic search using linq. I believe the syntax is
        correct, but cannot confirm it because when I try to cast my
        Session[“Employees”] from a List<to IQueryable<>, I get a cast error
        “Unable to cast object of type System.Collecti ons.Generic.Lis t to type
        System.Linq.IQu eryable”. Is there a way to cast List<to IQueryable<>, or is
        there a different way I need to be doing this?
        >
        protected void btnSearch_Click (object sender, EventArgs e)
        {
        try
        {
        if (Session["Employees"] != null)
        {
        IQueryable<Busi nessLogic.Servi ces.UserProfile employees =
        (IQueryable<Bus inessLogic.Serv ices.UserProfil e>)(List<Busine ssLogic.Service s.UserProfile>) Session["Employees"];
        >
        switch (ddSearchCriter ia.SelectedValu e)
        {
        case "UserName":
        employees.Where (c =c.Username ==
        txtSearchCriter ia.Text);
        break;
        >
        case "Email":
        employees.Where (c =c.EmailAddress ==
        txtSearchCriter ia.Text);
        break;
        >
        default: //LastName
        employees.Where (c =c.Person.LastN ame ==
        txtSearchCriter ia.Text);
        break;
        }
        >
        BindGrid(employ ees.Select(c =>
        c).ToList<Busin essLogic.Servic es.UserProfile> ());
        }
        else
        {
        List<BusinessLo gic.Services.Us erProfileemploy ees = null;
        Shared.Business Logic.UserSearc hFilter filter = new
        UserSearchFilte r();
        >
        switch (ddSearchCriter ia.SelectedValu e)
        {
        case "UserName":
        filter.Username = txtSearchCriter ia.Text;
        break;
        >
        case "Email":
        filter.EmailAdd ress = txtSearchCriter ia.Text;
        break;
        >
        default: //LastName
        filter.LastName = txtSearchCriter ia.Text;
        break;
        }
        >
        PagedResult<Bus inessLogic.Serv ices.UserProfil epagedResult =
        SearchEmployees (filter);
        employees =
        pagedResult.Res ults.ToList<Bus inessLogic.Serv ices.UserProfil e>();
        BindGrid(employ ees.ToList<Busi nessLogic.Servi ces.UserProfile >());
        }
        }
        catch (ThreadAbortExc eption) { }
        catch (Exception ex) { ExceptionHelper .Publish(ex); }
        }
        >

        Comment

        • =?Utf-8?B?TWlrZSBDb2xsaW5z?=

          #5
          RE: LINQ and dynamic where clauses

          So, it seems that I do not need to cast my Session object to an IQueryable
          object then...since that line does not work anyway. I can just cast it to a
          list<>, then put the results of the where into an IQueryable object at that
          time. Is that correct?

          "bruce barker" wrote:
          the where clause in a Linq query does not set a property, its a method
          (actually an extension method) that returns another IQueryable object that
          the Select method (which also return an IQueryable) can be called on. your
          code is throwing away the results of the Where.
          >
          >
          >
          >
          -- bruce (sqlwork.com)
          >
          >
          "Mike Collins" wrote:
          >
          I am trying to set up a dynamic search using linq. I believe the syntax is
          correct, but cannot confirm it because when I try to cast my
          Session[“Employees”] from a List<to IQueryable<>, I get a cast error
          “Unable to cast object of type System.Collecti ons.Generic.Lis t to type
          System.Linq.IQu eryable”. Is there a way to cast List<to IQueryable<>, or is
          there a different way I need to be doing this?

          protected void btnSearch_Click (object sender, EventArgs e)
          {
          try
          {
          if (Session["Employees"] != null)
          {
          IQueryable<Busi nessLogic.Servi ces.UserProfile employees =
          (IQueryable<Bus inessLogic.Serv ices.UserProfil e>)(List<Busine ssLogic.Service s.UserProfile>) Session["Employees"];

          switch (ddSearchCriter ia.SelectedValu e)
          {
          case "UserName":
          employees.Where (c =c.Username ==
          txtSearchCriter ia.Text);
          break;

          case "Email":
          employees.Where (c =c.EmailAddress ==
          txtSearchCriter ia.Text);
          break;

          default: //LastName
          employees.Where (c =c.Person.LastN ame ==
          txtSearchCriter ia.Text);
          break;
          }

          BindGrid(employ ees.Select(c =>
          c).ToList<Busin essLogic.Servic es.UserProfile> ());
          }
          else
          {
          List<BusinessLo gic.Services.Us erProfileemploy ees = null;
          Shared.Business Logic.UserSearc hFilter filter = new
          UserSearchFilte r();

          switch (ddSearchCriter ia.SelectedValu e)
          {
          case "UserName":
          filter.Username = txtSearchCriter ia.Text;
          break;

          case "Email":
          filter.EmailAdd ress = txtSearchCriter ia.Text;
          break;

          default: //LastName
          filter.LastName = txtSearchCriter ia.Text;
          break;
          }

          PagedResult<Bus inessLogic.Serv ices.UserProfil epagedResult =
          SearchEmployees (filter);
          employees =
          pagedResult.Res ults.ToList<Bus inessLogic.Serv ices.UserProfil e>();
          BindGrid(employ ees.ToList<Busi nessLogic.Servi ces.UserProfile >());
          }
          }
          catch (ThreadAbortExc eption) { }
          catch (Exception ex) { ExceptionHelper .Publish(ex); }
          }

          Comment

          Working...