2011-05-14 5 views
0

이 코드를 출력 할 때 중괄호없이 번호 만 원하면 {가격 : 1446}이됩니다. 이것을 할 수있는 방법이 있습니까? 또한 가격 가치를 얻은 후에 십진수으로 변환하고 싶습니다. 이것을 할 수있는 방법이 있습니까?LINQ 중괄호 제거, 캐스트 값

var flightPrice = (from f in XElement.Load(MapPath("flightdata.xml")).Elements("flight") 
          where (string)f.Element("flightnumber") == FlightNumber 
           && (string)f.Element("destinationairportsymbol") == Destination 
          select new { Price = (Int32)f.Element("price") } 
          ).SingleOrDefault(); 

lblPrice.Text = flightPrice.ToString(); 
+0

는 당신이 우리에게 당신이 분석하고있는 XML 파일을 보여줄 수의 select (Int32)f.Element("price")을 넣어? –

답변

1

select은 Price라는 단일 속성으로 익명 형식을 만듭니다. 당신이 원하는 모든이 실제 가격 (그리고 float로서) 인 경우, 그러나

// select new { Price = (Int32)f.Element("price") } 
select (float)(int)f.Element("price") 

에 선택을 변경, 당신이 가격과 같은 금융 뭔가를 처리하기 위해 float를 사용하지 않는 것이 좋습니다.decimal 데이터 형식이 이러한 값의 기본 형식입니다. 당신이 할 때 발생하는

select (decimal)f.Element("price") 
+0

예 십진수 두 자리까지 십진수를 사용하고 싶습니다. 어떻게 사용합니까? – multiv123

+0

@ multiv123, 답변을 업데이트했지만 단순히 float 대신 10 진수로 캐스팅하는 것입니다. –

+0

나는 그것을 사용하여 세금 값을 계산해야하기 때문에 십진수로 저장해야합니다. – multiv123

0

는 :

lblPrice.Text = flightPrice.Price.ToString(); 

또한이 작업을 수행 할 수 있습니다

// omited the first 3 lines of your linq statement... 
select (Int32)f.Element("price") 
).SingleOrDefault(); 

하는 경우 flightPrice 유형 int이 될 것입니다.

0

중 하나를 대신 select new { Price = (Int32)f.Element("price") }

또는

lblPrice.Text = flightPrice.Price.ToString();