0

I have a question about LinqToSql approach.

Model:

public class Person
{
    public int ID { get; set; }
    public string LastName { get; set; }
    public Phone Phone { get; set; }
}

public class Phone
{
    public int ID { get; set; }
    public string Number { get; set; }

    public bool IsUkr()
    {
        return this.Number.StartsWith("380");
    }
}

I use Nhibernate as ORM and I want to execute

IQueryable<Phone> phones = session.Query<Phone>().Where(x => x.IsUkr());

But Nhibernate can't translate this expression into sql because it doesn't know about method IsUkr().

Solution is to write some wrapper to IQueryable interface and use:

session.Query<Phone>().ToExpandable().Where ....

But I don't understand how translate LambdaExpression x => x.IsUkr() into
x => x.Number.StartsWith("380")

Can someone help me?

1 Answers1

0

Take a look at this answer: https://stackoverflow.com/a/13679572/5126411

IsUkr criteria is specific for the Phone entity so you can just add a static method like this:

public static Expression<Func<Phone, bool>> IsUkr = x => x.Number.StartsWith("380");

and use it:

IQueryable<Phone> phones = session.Query<Phone>().Where(Phone.IsUkr);

Maybe it would be better to create a special static class for such methods/properties.

Roman Koliada
  • 4,286
  • 2
  • 30
  • 59