2009-10-30 8 views
0

저는 linq을 SQL에 사용하기 시작했습니다. 누군가 foreq 루프가 실행될 때까지 linq-2-sql이 실행을 지연 시켰음을 확인할 수 있기를 바랍니다. 무엇보다이 코드가 비례하는지 누군가가 말해 줄 수 있습니까? 몇 가지 검색 매개 변수가있는 간단한 get 메소드입니다. 감사!Linq-2-Sql 코드 :이 배율을 조정합니까?

코드 :

public static IList<Content> GetContent(int contentTypeID, int feedID, DateTime? date, string text) 
     { 
      List<Content> contentList = new List<Content>(); 
      using (DataContext db = new DataContext()) 
      { 
       var contentTypes = db.ytv_ContentTypes.Where(p => contentTypeID == -1 || p.ContentTypeID == contentTypeID); 
       var feeds = db.ytv_Feeds.Where(p => p.FeedID == -1 || p.FeedID == feedID); 

       var targetFeeds = from f in feeds 
            join c in contentTypes on f.ContentTypeID equals c.ContentTypeID 
            select new { FeedID = f.FeedID, ContentType = f.ContentTypeID }; 

       var content = from t in targetFeeds 
           join c in db.ytv_Contents on t.FeedID equals c.FeedID 
           select new { Content = c, ContentTypeID = t.ContentType }; 

       if (String.IsNullOrEmpty(text)) 
       { 
        content = content.Where(p => p.Content.Name.Contains(text) || p.Content.Description.Contains(text)); 
       } 

       if (date != null) 
       { 
        DateTime dateTemp = Convert.ToDateTime(date); 
        content = content.Where(p => p.Content.StartDate <= dateTemp && p.Content.EndDate >= dateTemp); 
       } 

       //Execution has been defered to this point, correct? 
       foreach (var c in content) 
       { 
        Content item = new Content() 
        { 
         ContentID = c.Content.ContentID, 
         Name = c.Content.Name, 
         Description = c.Content.Description, 
         StartDate = c.Content.StartDate, 
         EndDate = c.Content.EndDate, 
         ContentTypeID = c.ContentTypeID, 
         FeedID = c.Content.FeedID, 
         PreviewHtml = c.Content.PreviewHTML, 
         SerializedCustomXMLProperties = c.Content.CustomProperties 
        }; 
        contentList.Add(item); 
       } 
      } 
      //TODO 
      return contentList; 
     } 

답변

0

는 '저울'로 무엇을 의미하는지에 따라 다릅니다. DB 측 큰 테이블을 다루는 경우이 코드는 문제를 일으킬 수 있습니다. SQL Server의 옵티마이 저는 where 절 술어에서 "or"연산자를 처리하는 데 실제로 좋지 않으며 여러 개가있는 경우 테이블 스캔으로 되돌아가는 경향이 있습니다. 나는 SQL 대신 테이블 스캔으로 돌아갈 가능성을 피하기 위해 U.Union 호출을 몇 개 사용했다.

기본 테이블과 그 데이터에 대한 자세한 내용을 공유 할 수 있다면보다 자세한 답변을 쉽게 얻을 수 있습니다.