3

I want to put an query inside an expression for a filter.

I have a switch and for a specific case I need to do a different validation.

private Expression<Func<MyClass, bool>> Filter(int idStatus)
{
    switch (idStatus)
    {
        case case1:
            return c => c.Status == (int)MyEnum.Status1 && c.Active;
        case case2:
            return c => c.Status == (int)MyEnum.Status2 && c.Active;
        case case3:
            return c => c.Status == (int)MyEnum.Status3 && c.Active;
        case case4:
            return c => _myService.FindByMyClassId(c.Id) != null && c.Active;
    }
}

MyClass:

namespace MyNamespace
{
    public class MyClass //Process class
    {
        public virtual int Id{ get; set; }
        public virtual string ProcessCode { get; set; }
        public virtual DateTime Created{ get; set; };
        public virtual int Status{ get; set; }
        public virtual int IdUser{ get; set; }
    }
}

MyClass mapping with Nhibernate:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="MyNamespace.MyClass,MyNamespace" table="my_class" lazy="true" mutable="false">

    <id name="Id" column="id" type="int32">
      <generator class="native"/>
    </id>
    <property column="process_code" type="String" name="ProcessCode" />
    <property column="created" type="DateTime" name="Created" />
    <property column="id_user" type="Int32" name="IdUser" />
    <property column="id_status" type="Int32" name="Status" />
    </class>
</hibernate-mapping>

This method is called in the main filter method:

query = query.Where(Filter(Convert.ToInt32(filters["Status"])));

With the normal cases the code runs perfectly but, in this specific case (case4) i'm getting the error:

variable 'c' of type 'MyClass' referenced from scope '', but it is not defined

Raul
  • 167
  • 9
  • 2
    Have you checked this https://stackoverflow.com/questions/30556911/variable-of-type-referenced-from-scope-but-it-is-not-defined. –  Aug 20 '19 at 14:52
  • 1
    I have, apparently this error is really generic. In that case the problem was two expressions with the same parameter name, but this doesn't apply to my problem. – Raul Aug 20 '19 at 14:56
  • 2
    Can you post 'MyClass' –  Aug 20 '19 at 15:00
  • 2
    Is the `query` related to Entity Framework? – Rabban Aug 20 '19 at 15:09
  • 1
    The query is related to Nhibernate. – Raul Aug 20 '19 at 15:13
  • 4
    As i remember right, NHibernate tries to translate the query into SQL but should not be able because of the service call. Did you tried the query without your filter method and added the predicate by hand like `query.Where(c => _myService.FindByMyClassId(c.Id) != null && c.Active)`? The error should be the same and indicate that this type of predicate is not working. In earlier version of NHibernate it threw some `NotSupportedException` or something like this, that was more meaningful. – Rabban Aug 20 '19 at 15:35
  • 1
    I don't think that it is possible to achieve it the way you want. If you don't have to many entities in your table, you could materialize them beforehand and then add your predicate so that it runs in your application. The downside is that you need to request all entities and then filter them. Or you need to rework your service to return a list of valid ids and then put them in your query. – Rabban Aug 20 '19 at 15:39
  • 1
    Perhaps the issue lies in your `FindByMyClass` method or your `.ID` property? –  Aug 20 '19 at 15:46
  • Rabban, that's what I thought, I"ll probabbly need to make a query (q2) listing all entities in the table using the service and then remove all the entities in the previus query that are not in q2. As you said, when I tried ```query.Where(c => _myService.FindByMyClassId(c.Id) != null && c.Active)``` the result is the same error. – Raul Aug 20 '19 at 16:02
  • xTwisteDx, I don't think that there's any issue with the class, i've been using it for some time and this is the first time i'm getting any error. – Raul Aug 20 '19 at 16:03
  • Anyway, thanks for the help. – Raul Aug 20 '19 at 16:04

0 Answers0