implementing effective searching in the business[maybe with linq?]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • giddy

    implementing effective searching in the business[maybe with linq?]

    hi,

    Yes its a design question again. =)

    If I have something like:
    class Person
    {
    //functions:
    static Person[] GetAllPersons() ;
    static Person[] Search(string field,string value);
    }

    In the second function I would do something like build an sql query
    like this:
    >SELECT ........... WHERE field = value
    and send it through another data class to retrieve the right Persons.

    eg:
    Person[] teens = Person.Search(" Age",18);

    Firstly the biggest problem searching with AND, >,<, OR, Between etc.?
    Which terribly limits the search.I could make more functions for that,
    but its turns into a little bit of a mess. Is there a better/more
    generic way to build a query on the fly?

    Would I benefit by using Linq instead/would it be more efficient to
    load all persons in memory and then search them with linq? If I were
    to use linq how again would I write a function to build a query on the
    fly?

    Thanks so much

    Gideon
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: implementing effective searching in the business[maybe with linq?]

    giddy wrote:
    hi,
    >
    Yes its a design question again. =)
    >
    If I have something like:
    class Person
    {
    //functions:
    static Person[] GetAllPersons() ;
    static Person[] Search(string field,string value);
    }
    >
    In the second function I would do something like build an sql query
    like this:
    >SELECT ........... WHERE field = value
    and send it through another data class to retrieve the right Persons.
    >
    eg:
    Person[] teens = Person.Search(" Age",18);
    >
    Firstly the biggest problem searching with AND, >,<, OR, Between etc.?
    Which terribly limits the search.I could make more functions for that,
    but its turns into a little bit of a mess. Is there a better/more
    generic way to build a query on the fly?
    >
    Would I benefit by using Linq instead/would it be more efficient to
    load all persons in memory and then search them with linq? If I were
    to use linq how again would I write a function to build a query on the
    fly?
    >
    Thanks so much
    >
    Gideon
    You can make a query or stored procedure that takes various parameters,
    and write the conditions so that it can use null values in the parameters.

    Example:

    create procedure Person_Search
    @LastName varchar(200)
    @AgeFrom int,
    @AgeTo int
    as
    select FirstName, LastName, Age
    from Person
    where LastName = isnull(@LastNam e, LastName)
    and Age between isnull(@AgeFrom , 0) and isnull(@AgeTo, 1000)

    Now you just send null values for the parameters that you don't want to use.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • William Stacey

      #3
      Re: implementing effective searching in the business[maybe with linq?]

      If using EF, you can use esql string predicates like so and get sql
      operators such as like, and, or, etc.

      private void button6_Click(o bject sender, EventArgs e)
      {
      var u = Search("it.Tag like '%illiam' and it.IsLocal=true ");
      ObjectDumper.Wr ite(u);

      var u2 = Search("it.User ID=1");
      ObjectDumper.Wr ite(u2);
      }

      private IEnumerable<Tes t.UsersSearch(s tring predicate)
      {
      using (Test.TestDB db = new Test.TestDB())
      {
      var q = db.Users.Where( predicate).Sele ct(u =u);
      return q.ToList();
      }
      }

      If using L2S, you can download dynamic library:


      --William

      "giddy" <gidisrael@gmai l.comwrote in message
      news:f55c2efc-ac93-437c-9380-03487bd482c4@v1 3g2000pro.googl egroups.com...
      hi,
      >
      Yes its a design question again. =)
      >
      If I have something like:
      class Person
      {
      //functions:
      static Person[] GetAllPersons() ;
      static Person[] Search(string field,string value);
      }
      >
      In the second function I would do something like build an sql query
      like this:
      >>SELECT ........... WHERE field = value
      and send it through another data class to retrieve the right Persons.
      >
      eg:
      Person[] teens = Person.Search(" Age",18);
      >
      Firstly the biggest problem searching with AND, >,<, OR, Between etc.?
      Which terribly limits the search.I could make more functions for that,
      but its turns into a little bit of a mess. Is there a better/more
      generic way to build a query on the fly?
      >
      Would I benefit by using Linq instead/would it be more efficient to
      load all persons in memory and then search them with linq? If I were
      to use linq how again would I write a function to build a query on the
      fly?
      >
      Thanks so much
      >
      Gideon

      Comment

      Working...