开发者

Entity & LINQ Method Chain Query

开发者 https://www.devze.com 2023-02-07 07:17 出处:网络
Ok, lets say that below is my database structure that I have imported into an Entity Model: Place ----------------

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 =开发者_StackOverflow中文版> t.DayOfWeeks.Any(z => z.Day = request.DayOfWeek)));
                return placereturn;
            }
        }

All works except for the Day Of Week line.


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.


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!


I think you mean the question marks:

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

And so on...

0

精彩评论

暂无评论...
验证码 换一张
取 消