Select records according to ID with EntityFrameWork

0

New work and the test is as follows

I have this function that receives a parameter of type (DbGeography) which selects a set of results and returns a list, now what I need is to change the parameter by (int RestaurantID) and make the same selection but according to this field (I know .. It may not be necessary to return a list since only one record is going to be selected ... but it's for now to be done that way ... then I have to change it)

public List<VistaRestaurantSearch> SearchRestaurant(DbGeography geografi)
        {
            List<VistaRestaurantSearch> vista = new List<VistaRestaurantSearch>();
            var rest = (from de in se.DeliveryConfiguration join re in se.restaurant on de.Restaurantid equals re.RestaurantID
                        where (de.Position.Distance(geografi)/1000) < de.Distance select re).ToList();

            foreach (var item in rest)
            {
                vista.Add(new VistaRestaurantSearch() {
                    restaurant = item,
                    config = se.DeliveryConfiguration.Where(a => a.Restaurantid == item.RestaurantID).FirstOrDefault(),
                    Horary = new EngineRestaurant().getRestaurantHourbyRestaurant(item.RestaurantID),
                    prom = new EngineReview().getReviewAverage(item.RestaurantID)
                });

            }

            return vista;

        }

and these my tables

    
asked by Efrain Mejias C 23.09.2016 в 17:31
source

1 answer

2

You could implement something like being

public List<VistaRestaurantSearch> SearchRestaurant(int RestaurantID)
{

    var query = (from de in se.DeliveryConfiguration 
                join re in se.restaurant on de.Restaurantid equals re.RestaurantID
                where re.RestaurantID == RestaurantID
                select new{
                    Delivery = se,
                    Restaurant = re
                }).ToList();

    var rest = query.Select(x=> new VistaRestaurantSearch()
                        {
                            restaurant = x.Restaurant,
                            config = se.DeliveryConfiguration.Where(a => a.Restaurantid == x.Restaurant.RestaurantID).FirstOrDefault(),
                            Horary = new EngineRestaurant().getRestaurantHourbyRestaurant(x.Restaurant.RestaurantID),
                            prom = new EngineReview().getReviewAverage(x.Restaurant.RestaurantID)
                        }).ToList();

    return rest;

}

you do not need to perform a foreach of the answer of the linq to transform into another entity, you do it directly in select

In addition to completing the properties of Horary and prom you can not perform a join with the other tables to solve it, so you do not have to go to the other classes and it is resolved in a single query

    
answered by 23.09.2016 / 17:55
source