2012-02-23 2 views
5

나는 다음과 같은 코드가 작동하지 않습니다LINQ - 동적 ORDERBY 절은

//build query 
var shops = (from p in dataContext.shops 
let distance = dataContext.GetDistance(p.lat, p.lon, nearlat,nearlon) 
        join c in dataContext.shops_category on p.id equals c.poi_id 
        select new ShopsModel { p = p, distance = distance } 
         ); 
     } 
//add dynamic orderby 
if(somthig) 
    shops.OrderBy(distance) 
else 
    shops.OrderBy(p.name) 


//get records. 
return shop.Take(30).ToList() 

그건이있는 OrderBy를 제외하고 잘 작동합니다. 생성 된 SQL 코드에는 orderby 절이 없으며 레코드는 정렬되지 않습니다.

아이디어가 있으십니까? 도와 주셔서 감사합니다.

답변

5

있는 OrderBy가 기본이되는 데이터를 변이하지 않습니다 - 그것은 적절한 순서로 열거 가능한 반환합니다. 결과를 다시 상점에 할당해야합니다.

if (someCondition) 
{ 
    shops = shops.OrderBy(shop => shop.distance); 
} 
else 
{ 
    shops = shops.OrderBy(shop => shop.p.name); 
} 
1

이 시도 :

Shops=shops.OrderBy(distance);