2

I have an application written using C# on the top of the ASP.NET MVC 5 framework. In addition, I am using EntityFramework 6.2 as an ORM to interact with my data.

I wrote the following join statement using Fluent LINQ

List<string> names = new List<string>{"", ....};
query = TopLevelQuery.Where(x => x.EndedAt.HasValue && x.StartedAt >= startedAt && x.EndedAt.Value <= endedAt)
                     .Join(UserService.QueryUniqueRecords(),
                     entry => entry.UserId,
                     rec => rec.UserId,
                     (entry, rec) => new { entry, rec })
                     .Where(result => result.entry.IsEqualDateOf(result.rec.DayOf) 
                         && names.Contains(result.rec.Name))
                     .Select(x => x.entry);

However, I get the following error during runtime

LINQ to Entities does not recognize the method 'IsEqualDateOf', and this method cannot be translated into a store expression.

Here is my IsEqualDateOf extension method

public static class MyModelExtensions
{
    public static DateTime GetLocalDate(this MyModel entry)
    {
        var local = DbFunctions.TruncateTime(SqlFunctions.DateAdd("ss", entry.UtcOffset, entry.StartedAt));

        return local.Value;
    }

    public static bool IsEqualDateOf(this MyModel entry, DateTime dateOf)
    {
        bool isEqual = entry.GetLocalDate().Equals(dateOf.Date);

        return isEqual;
    }
}

However, if I convert my LINQ expression to the following pseudo, it works as expected

query = TopLevelQuery.Where(x => x.EndedAt.HasValue && x.StartedAt >= startedAt && x.EndedAt.Value <= endedAt)
                     .Join(UserService.QueryUniqueRecords(),
                     entry => entry.UserId,
                     rec => rec.UserId,
                     (entry, rec) => new { entry, rec })
                     .Where(result => DbFunctions.TruncateTime(SqlFunctions.DateAdd("ss", entry.UtcOffset, entry.StartedAt)) == result.rec.DayOf 
                         && names.Contains(result.rec.Name))
                     .Select(x => x.entry);

But, I want to be able to reuse the same logic in multiple places within my project, which is why I want to extract it into some kind of extension or method.

How, can I extract the DbFunctions and SqlFunctions call into a reusable method in which can be used in LINQ before AsEnumerable() is called?

UPDATED

I also tried to extract the logic into lambda expression by adding the following code to MyModel class

public class MyModel
{
    public DateTime StartedAt { get; set; }
    public int UtcOffset { get; set; }
    // ...

    public Expression<Func<MyModel, bool>> IsDateOf(DateTime dayOf)
    {
        return p => p.StartedAt.AddSeconds(p.UtcOffset) == dayOf.Date;
    }
}

Then I tried to consume it like so

query = TopLevelQuery.Where(x => x.EndedAt.HasValue && x.StartedAt >= startedAt && x.EndedAt.Value <= endedAt)
                     .Join(UserService.QueryUniqueRecords(),
                     entry => entry.UserId,
                     rec => rec.UserId,
                     (entry, rec) => new { entry, rec })
                     .Where(result => result.entry.IsDateOf(result.rec.DayOf) 
                         && names.Contains(result.rec.Name))
                     .Select(x => x.entry);

But that throws the following syntax error when trying to consume it

Operator '&&' cannot be applied to operands of type 'Expression<Func<MyModel,bool>>' and bool.

Moreover, I tried making the IsDateOf expression static and called it like so

query = TopLevelQuery.Where(x => x.EndedAt.HasValue && x.StartedAt >= startedAt && x.EndedAt.Value <= endedAt)
                     .Join(UserService.QueryUniqueRecords(),
                     entry => entry.UserId,
                     rec => rec.UserId,
                     (entry, rec) => new { entry, rec })
                     .Where(result => result.entry.Where(MyModel.IsDateOf(result.rec.DayOf))
                         && names.Contains(result.rec.Name))
                     .Select(x => x.entry);

But that gives me the following syntax error

'MyMode' does not contain a definition for Where and the best extension method overload Queryable.Where<MyModel>IQueryable<MyModel>, Expression<Func<MyModel, bool>>) required a reciever of type IQueryable

14
  • AFAIK you have to make it a lambda expression instead of a method, something like Expression<Func<Datetime, bool>>, and it can't have a body. See this question for an example of what I mean.
    – user47589
    Dec 26, 2018 at 20:55
  • @Amy I have actually attempted that. Please check my updated question.
    – Junior
    Dec 26, 2018 at 21:07
  • 1
    You are calling a method that returns an expression. You are not invoking the expression.
    – user47589
    Dec 26, 2018 at 21:09
  • Why are you now trying to call result.entry.Where? Get rid of that.
    – user47589
    Dec 26, 2018 at 21:22
  • 1
    You now have, for some reason, .Where(result => result.entry.Where(MyModel.IsDateOf(result.rec.DayOf)) && names.Contains(result.rec.Name)) Change that to .Where(result => MyModel.IsDateOf(result.entry, result.rec.DayOf) && names.Contains(result.rec.Name))
    – user47589
    Dec 26, 2018 at 21:25

1 Answer 1

2

Remove the parameter from your method and make the expression accept two parameters:

public static Expression<Func<MyModel, DateTime, bool>> IsDateOf 
    = (MyModel p, DateTime d) => p.StartedAt.AddSeconds(p.UtcOffset) == d.Date;

Note I have made it a static field, not a method. Then in your LINQ expression, you need to invoke it:

MyModel.IsDateOf(result.entry, result.rec.DayOf)

If you don't make it a field (or property), you have to first invoke the method to get the expression; then you need to invoke the expression:

MyModel.IsDateOf()(result.entry, result.rec.DayOf)

Which, in my opinion, looks weird.

4
  • Thank you! When calling MyModel.IsDateOf(result.entry, result.rec.DayOf) I get the following error Non-invocable member 'MyModel.IsDateOf' cannot be used like a method. Here is how I am calling it (entry, rec) => new { entry, rec}).Where(result => MyModel.IsDateOf(result.entry, result.rec.DayOf) && names.Contains(result.rec.Name)). Not sure what I am missing this time.
    – Junior
    Dec 26, 2018 at 21:59
  • Can you tell me whether the overload of Where you're using is taking Expression<> objects as parameters, or actual Func<> objects?
    – Ann L.
    Dec 26, 2018 at 22:04
  • I think that @Amy's answer is assuming that the Where overload you're using has a parameter of Expression<Func<MyModel,bool>>, but that other things in the call are forcing it to use the overload that takes plain Func<MyModel, bool>. That would cause it to object: it wants a method, a Func<>, and it's been given an Expression<Func<>>.
    – Ann L.
    Dec 26, 2018 at 22:09
  • From what I can tell, .Where(result => ) accepts IQueryable<'a> IQueryable<'a>.Where<'a>(Expression<Func<a', bool>> predicts) a' is new { MyModel entry, MyRecord rec}
    – Junior
    Dec 26, 2018 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.