2017-04-06 1 views
0
var lstCatalog = context.Drinks_Category.Select(x => new { x.Id_category, x.Name_category, x.Parent}).ToList(); 

나는 3 개의 표가 있습니다. 시퀀스 번호 열을 추가하고 싶습니다. 어떻게해야합니까? Id_category가 Key입니다.linQ에서 번호순을 선택하십시오.

고마워요!

내 새로운 코드 :

var lstCatalog = context.Drinks_Category.Select((x, i) => new { Index = i, x.Id_category, x.Name_category, x.Parent }).OrderByDescending(x=>x.Id_category).ToList(); 

이 오류 :

System.NotSupportedException : System.Linq.IQueryable 1[<>f__AnonymousType1 4 '엔티티에 LINQ는 방법을 인식하지 못하는'[선택 System.Int32, 시스템 [BBModel.Drinks_Category], System.Linq.Expressions.Expression 1[System.Func 3 [BBModel.Drinks_Category, System.Int32, <> f__AnonymousType1`4]을 선택하십시오. [System.Int32, System.String, System.String, System. 문자열]]]) '메서드를 호출하고이 메서드는 저장소 식으로 변환 할 수 없습니다.'

답변

3

Select extension that allows you to including the iteration index의 과부하가 있습니다. 선택의 람다에 추가하기 만하면됩니다. 이것을 OrderBy과 처음으로 결합 할 수 있습니다. 그렇지 않으면 저장소 순서대로 (Drinks_Category 클러스터 된 색인으로) 가져옵니다.

var lstCatalog = context.Drinks_Category.Select((x, i) => new { i, x.Id_category, x.Name_category, x.Parent}).ToList(); 
+0

나는이 질문을 업데이트했다. 제 실수를 바로 잡으세요. –

관련 문제