开发者

Help to build LINQ query

I have S开发者_如何学运维QL database as follows

alt text http://img97.imageshack.us/img97/5774/dbimage.jpg

Now I want to filter the restaurant_detail table for the parameters: 1. cuisine 2. area

Can you help me to build LINQ query?


I presume you have a model generated either with LINQ to SQL or Entity Framework. Also, I'm assuming foreign key relationships have been set.

var details = db
    .Cuisines
    .Where(c => c.Cuisine=="something")
    .SelectMany(c => c.RestaurantCuisines)
    .Select(rc => rc.Restaurant.RestaurantDetails)
    .Where(rd => rd.Area=="something")
;


Done with the linq query using following lines of code :

            c = from q in dc.restaurant_cuisines
                    where q.cuisine.cuisine1.Contains(cuisine)
                        && q.restaurant.price.ToString().Length == price.Length
                    select new NearBy { NearById = q.restaurant.id, NearByLongitude = (double)q.restaurant.longitude, NearByLatitude = (double)q.restaurant.latitude };
        }
        int[] ids = new int[c.Count()];

var lon = from q1 in dc.area_maps where q1.area.ToLower() == area.ToLower() select q1.longtitude;
        var lat = from q1 in dc.area_maps where q1.area.ToLower() == area.ToLower() select q1.latitude;
        foreach(NearBy n in c)
        {
            result = calcDistNew((double)lat.FirstOrDefault(), (double)lon.FirstOrDefault(), n.NearByLatitude, n.NearByLongitude);
            ids[i++] = n.NearById;
        }


        var r = from q in dc.restaurant_details
                where 1 == 1 &&
                (ids).Contains(q.restaurant_id)
                select new Restaurant
                {
                    Restora_id = q.restaurant_id.ToString(),
                    Name = q.restaurant.name,
                    Foodtype = q.restaurant.foodtype.foodtype1,
                    Avg_rating = q.restaurant.avg_rating.ToString(),
                    Featured = q.restaurant.featured.ToString(),
                    CuisineList = getCuisine(q.restaurant_id),
                    Restora_type = q.type,
                    Distance = Math.Round(calcDistNew((double)lat.FirstOrDefault(), (double)lon.FirstOrDefault(), (double)q.restaurant.latitude, (double)q.restaurant.longitude), 2),
                    Newarrival = q.restaurant.newarrival.ToString(),
                    CountRecord = ids.Length.ToString()
                };
        var d = r.AsEnumerable().OrderBy(t => t.Distance);
        var g = d.Take(recordSize + 10).Skip(recordSize);
        return g.ToList();

Please note that above displayed code generated with some changes from the initial requirements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜