LINQ 의 유연함

December 26, 2007 23:49 by WebStoryMaker

Visual Studio 2008 에서 새롭게 선보이고 있는 (.NET Framework 3.5 인 C# 3.0 과 VB9) LINQ (Language INtegrated Query)에 대해 흔히 들어본 일이 있을 것이다. LINQ 가 SQL 을 대체하는 언어로 생각을 하기 쉽상인데 사실 LINQ 는 SQL 외에도 유용하게 쓰일 수 있다.

실례로 2.0에서 다음과 같은 코드를 생각할 수 있다.

foreach (Control myControl in this.Controls)
{
      if(myControl is TextBox)
      {
         myControl.Enabled = false;
      }
}

위의 코드는 페이지에 TextBox 콘트롤을 Disable 시키는 코드이다. 보시다시피 모든 콘트롤을 foreach 루프를 통해 검색을 해야 하는 단점을 지니고 있다. 이러한 코드를 LINQ 는 개선 시킬 수 있다.

var myTextBoxes = from myControl in this.Controls
where myConrol is TextBox
select myControl;

foreach (TextBox myTextBox in myTextBoxes)
{
       myTextBox.Enabled = false;
}

코드는 더 길어졌지만 불필요한 검색을 제거했으므로 더 효율적으로 사용할 수 있음을 알 수 있다.


Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: LINQ
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

September 7. 2008 14:36