Cyberdrew
Cyberdrew

Reputation: 1842

Entity & LINQ Method Chain Query

Ok, lets say that below is my database structure that I have imported into an Entity Model:

Place
----------------
Id  bigint PK
Name  varchar(10)

Time
----------------
Id  bigint PK
PlaceId  bigint FK_Place_Id
StartTime datetime
EndTime  datetime

DayOfWeek
----------------
Id  bigint PK
Name  varchar(10)

TimeDayOfWeek
----------------
TimeId  bigint PK FK_Time_Id
DayOfWeekId bigint PK FK_DayOfWeek_Id

In a LINQ method chain I would like to do something similar to the following:

public List<Place> GetPlaces(SearchRequest request)
        {
            using(var c = new Context())
            {
                var placereturn = c.Places.AsEnumerable();

                if (request.StartTime.HasValue)
                    placereturn = c.Places.Where(s => s.Time.Any(t => t.StartTime >= request.StartTime));
                if (request.EndTime.HasValue)
                    placereturn = c.Places.Where(s => s.Time.Any(t => t.EndTime >= request.EndTime));
                if (request.DayOfWeek.HasValue)
                    placereturn = c.Places.Where(s => s.Time.Any(t => t.DayOfWeeks.Any(z => z.Day = request.DayOfWeek)));
                return placereturn;
            }
        }

All works except for the Day Of Week line.

Upvotes: 0

Views: 776

Answers (3)

Cyberdrew
Cyberdrew

Reputation: 1842

public List<Place> GetPlaces(SearchRequest request)
        {
            using (var c = new Context())
            {
                var placereturn = c.Places.AsEnumerable();
                if (request.StartTime.HasValue)
                    placereturn = c.Places.Where(s => s.Time.Any(t => t.StartTime >= request.StartTime));
                if (request.EndTime.HasValue)
                    placereturn = c.Places.Where(s => s.Time.Any(t => t.EndTime >= request.EndTime));
                if (request.DayOfWeek.HasValue)
                    placereturn = c.Places.Where(s => s.Time.Any(t => t.DayOfWeeks.Any(z => z.Name == request.DayOfWeek.Value)));
                return placereturn;
            }
        }

I found it, this works!

Upvotes: 1

Konstantin G.
Konstantin G.

Reputation: 3

I think you mean the question marks:

if (request.StartTime.HasValue)
            placereturn.Where(r => r.StartTime >= DateTime.Now);

And so on...

Upvotes: 0

Geoff
Geoff

Reputation: 9340

You're close already to what I think you are after:

Public List<Place> GetPlaces(SearchRequest request)
    {
        using(var c = new Context())
        {
            var placereturn = c.Places;

            if (request.StartTime.HasValue)
                placereturn = placeretun.Where(???); //Any place that has a start time greater than or equal to the search start time
            if (request.EndTime.HasValue)
                placereturn = placeretun.Where(???);//Any place that has a end time less than or equal to the search end time
            if (request.DayOfWeek.HasValue)
                placereturn = placeretun.Where(???);//Any place where the day of week = the search day of week
            return placereturn.ToList();
        }
    }

You can just keep appending to your query. It won't be evaluated until it is actually used. In this case, it will be evaluated when you call ToList on the return.

Upvotes: 1

Related Questions