Entity & LINQ Method Chain Query - PullRequest
       3

Entity & LINQ Method Chain Query

0 голосов
/ 27 января 2011

Хорошо, допустим, что ниже представлена ​​структура моей базы данных, которую я импортировал в 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

В цепочке методов LINQ я хотел бы сделать что-то похожее на следующее:

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;
            }
        }

Все работы, кроме строки «День недели».

Ответы [ 3 ]

1 голос
/ 27 января 2011
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;
            }
        }

Я нашел это, это работает!

1 голос
/ 27 января 2011

Вы уже близки к тому, что, я думаю, вам нужно:

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();
        }
    }

Вы можете просто продолжать добавлять свой запрос.Он не будет оцениваться, пока не будет фактически использован.В этом случае он будет оцениваться при вызове ToList для возврата.

0 голосов
/ 27 января 2011

Я думаю, вы имеете в виду вопросительные знаки:

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

И так далее ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...