none
need help on linq sql syntax, custom filter RRS feed

  • Question

  • I want filter records with below approach:

          var result = from i in dc.GetTable<II>             
                 where IsTrue(myParam01, i.Bool) ...
    
        public bool IsTrue(MyParam p1, bool isFld)
        {
          switch (p1)
          {
            case MyParam.IsTrue:
              return isFld;
    
            case MyParam.IsFalse:
              return !isFld;
    
    //        case MyParam.WhoCare:
          }
    
          return true;
        }
    
    

    I got this "no supported translation to sql" error. I know why the error, and i believe there's a way to get it done, but dunno the proper syntax.

    I properly need to change the IsTrue function to something like below:

        public Expression<Func<II, bool>> IsFld(MyParam p1)
        {
          switch (p1)
          {
            case MyParam.IsTrue:
              return (dm => dm.IsFld);
    
            case MyParam.IsFalse:
              return (dm => !dm.IsFld);
          }
    
          return (dm => true);
        }
    

    but i can't get it work, can someone point me what i'm missing/to-chg?

     

    • Moved by SamAgain Tuesday, August 17, 2010 9:55 AM not bcl question (From:.NET Base Class Library)
    Monday, August 16, 2010 9:52 AM

Answers

  • IQueryable<II> query= dc.GetTable<II>().AsQueryable();
    query.Where(isFld(myParam01));
    
    //query.Where( Expression<Func<II, bool>> )
    
    List<II> results = query.ToList();
    

    Use what Jesse say for your method stubs.  I am guessing you asking about the query since yours didn't compile.  I hope this helps.

     

    Friday, August 20, 2010 2:19 PM

All replies

  • This makes it compile, but I don't really understand what you're trying to accomplish... Is this what you're looking for?

     

        public class MyParam
        {
          public bool IsTrue
          {
            get;set;
          }
    
          public bool IsFalse
          {
            get;set;
          }
        }
    
        public class II
        {
          public bool IsFld { get; set; }
        }
    
        public Expression<Func<II, bool>> IsFld(MyParam p1)
        {
          if (p1.IsTrue)
          {
            return (dm => dm.IsFld);
          }
          else if (p1.IsFalse)
          {
            return (dm => !dm.IsFld);
          }
          else
          {
            return (dm => true);
          }
        }
    

    Monday, August 16, 2010 12:27 PM
  • Hi,

    Thanks for your post. Please consider LINQ to SQL forum as a better option.


    Please mark the right answer at right time.
    Thanks,
    Sam
    Tuesday, August 17, 2010 9:52 AM
  • IQueryable<II> query= dc.GetTable<II>().AsQueryable();
    query.Where(isFld(myParam01));
    
    //query.Where( Expression<Func<II, bool>> )
    
    List<II> results = query.ToList();
    

    Use what Jesse say for your method stubs.  I am guessing you asking about the query since yours didn't compile.  I hope this helps.

     

    Friday, August 20, 2010 2:19 PM