2013-11-09 2 views
0

내 테이블에서 선택된 행의 ItemNumber, ItemDescription, UnitDescription이라는 3 개의 열 값을 가져 오는 데 사용되는 메서드가 있습니다. 선택한 행의 ItemId 매개 변수를 전달하고 다른 변수의 각 열 값을 가져와야합니다. 내가 모르는 사람 .Can 그것을 바탕으로 일을하는 것은 수행하는 방법을 가르쳐 방법 this.Here 내 코드 -이다이 메소드에서 열 값을 가져 오는 방법은 무엇입니까?

Satpal이 좋은 예를 준
public System.Collections.IEnumerable FetchDataItem(int id) 
    { 
     var row = (from t1 in context.InventoryUnit 
        join t2 in context.InventoryItem on t1.UnitId equals t2.UnitId 
        where t2.ItemId == id 
        orderby t2.ItemNumber, t2.ItemDescription 
        select new 
        { 

        t2.ItemNumber, 
        t2.ItemDescription, 
        t1.UnitDescription 
        }); 


     return row.ToList(); 
    } 
+0

하는대로'는 IEnumerable '에 반환 유형을 설정해야합니다'는 IEnumerable ' –

답변

0

, 그러나 당신이 클래스를 생성하지 않으려면 이것을 사용하여 익명 유형으로 선택할 수 있습니다.

public static void Main() 
{ 
    // Short example... 
    var rows = (from t1 in context.InventoryUnit 
       join t2 in context.InventoryItem on t1.UnitId equals t2.UnitId 
       where t2.ItemId == id 
       orderby t2.ItemNumber, t2.ItemDescription 
       select new 
       { 
        ItemNumber = t2.ItemNumber, 
        ItemDescription = t2.ItemDescription, 
        UnitDescription = t1.UnitDescription 
       }); 

    // Here you still have access to data and the types of the data. 

    foreach (var row in row) 
    { 
     // do something with the data.... 
     var someint = row.ItemNumber; 
     var someString = row.ItemDescription; 
     var someotherstring = row.ItemDescription; 
    } 
} 
+0

나는 (ItemNumber에 대한) int 형, 문자열의 3 개 가지 변수 (각 컬럼에 의해 반환되는 값을 어떻게 부여하는지 ItemDescription) 및 문자열 (UnitDescription의 경우) – user2718073

+0

@ user2718073이 (가) 업데이트되었습니다. IEnumerable 반환에 대한 설명은 무시하십시오. 이 방법을 사용하려면 동일한 방법을 사용해야합니다. –

관련 문제