0

I'd like to wrap the SqlFunctions.IsNumeric function so that I can use it in other layer without the need to add reference to System.Data (no matter why).
Here is what I did - In my Repository Interfaces:

public interface IUtils
{
    Expression<Func<bool>> IsNumeric(string str);
}

In my ef layer:

public class EFUtils : IUtils
{
    public Expression<Func<bool>> IsNumeric(string str)
    {
        return () => SqlFunctions.IsNumeric(str) == 1;
    }
}

In my business later:

public class RepositoryUtils
{
    protected static IUtils Utils { get; set; }

    static RepositoryUtils()
    {
        Utils = ObjectFactory.GetInstance<IUtils>(); //Using structureMap
    }

    public static Expression<Func< bool>> IsNumeric(string str)
    {
        return Utils.IsNumeric(str);
    }
}

The problem is that now, after I exposed the method, I don't really know how to use it..
It is something like:

Customers.OrderBy(x=>**Order by is numeric on x.Name);

Any idea?

Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
Naor
  • 23,465
  • 48
  • 152
  • 268
  • Sorry I don't know but can you tell me whatis the "=>" called, what does it do? I've not seen this syntax. – mikey Jun 02 '11 at 00:20
  • @mikey: It is actually syntax of lambda expression which is syntactic suagr to delegates. More info: http://msdn.microsoft.com/en-us/library/bb397687.aspx – Naor Jun 02 '11 at 00:23

3 Answers3

0

Something like that ?

Customers.OrderBy(x=>RepositoryUtils.IsNumeric(x.Name));
Eugen
  • 2,934
  • 2
  • 26
  • 47
0

The RepositoryUtils returns Expression<Func<bool>>, so it should be like this:

Customers.OrderBy(x => RepositoryUtils.IsNumeric(x.Name).Compile()());

By this code you compile the lambda expression and then call it.

UPD. I don't see for what purpose Expression<Func<bool>> is used. I'd use a simple bool:

public class EFUtils : IUtils
{
    public bool IsNumeric(string str)
    {
        return SqlFunctions.IsNumeric(str) == 1;
    }
}

public class RepositoryUtils
{
    protected static IUtils Utils { get; set; }

    static RepositoryUtils()
    {
        Utils = ObjectFactory.GetInstance<IUtils>(); //Using structureMap
    }

    public static bool IsNumeric(string str)
    {
        return Utils.IsNumeric(str);
    }
}

Then the lambda expression will be simpler:

Customers.OrderBy(x => RepositoryUtils.IsNumeric(x.Name));
Centro
  • 3,892
  • 2
  • 25
  • 31
  • @Centro: I get the exception: This function can only be invoked from LINQ to Entities... – Naor Jun 02 '11 at 00:47
  • @Naor Where exactly do you have an exception? – Centro Jun 02 '11 at 00:51
  • @Centro: Later I do query.Count() for example and this throws the exception. Your update is better but I still get this exception: This function can only be invoked from LINQ to Entities. I am using lambda and the exception explains I need to use linq to entities instead. Do you know how to work around this so I can use lambda? – Naor Jun 02 '11 at 01:17
  • @Naor There seem to be specifics for lambda expressions for Entity Framework there. Please see [this post](http://stackoverflow.com/questions/5845993/linq-to-entities-doesnt-recognize-a-method) for more information. – Centro Jun 02 '11 at 09:26
  • 1
    @Centro: The link you gave me is a question I asked and I can't find there something that will help me.. – Naor Jun 02 '11 at 10:10
  • @Naor Did you try to directly use `SqlFunctions.IsNumeric` - `Customers.OrderBy(x => SqlFunctions.IsNumeric(x.Name) == 1)` - does it work? – Centro Jun 02 '11 at 11:36
0

I think it doesn't work this way. I believe the expression itself should look like:

Expression<Func<T, bool>> YourNewExpression(T something)

not only

Expression<Func<bool>> YourNewExpression(T something)

that is nothing. If you want to pass expression to OrderBy you don't pass:

.OrderBy(x => YourNewExpression(x))

You must pass:

.OrderBy(YourNewExpression)

Which leads to a problem in reusability because if you want to pass string to the expression you can use it only on IQueryable<string>. I'm not sure if it can be somehow tweaked to work. This is just explanation why it doesn't work.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670