2010-04-14 5 views
3

Linq2Sql 데이터 컨텍스트에서 데이터베이스를 업데이트하거나 삭제하기 위해 다시 연결해야하는 연결이 끊어진 Linq2Sql 개체 또는 키 목록이있는 경우가 종종 있습니다. 이것이 SQL이라면 WHERE 절에 IN을 사용할 것이지만 Linq2Sql에서 무엇을 해야할지 고민하고 있습니다.Linq2Sql을 사용하여 "In"을 시뮬레이트하는 방법

public void MarkValidated(IList<int> idsToValidate) 
{ 
    using(_Db.NewSession()) // Instatiates new DataContext 
    { 
     // ThatAreIn <- this is where I am stuck 
     var items = _Db.Items.ThatAreIn(idsToValidate).ToList(); 
     foreach(var item in items) 
      item.Validated = DateTime.Now; 
     _Db.SubmitChanges(); 
    } // Disposes of DataContext 
} 

또는 :

public void DeleteItems(IList<int> idsToDelete) 
{ 
    using(_Db.NewSession()) // Instatiates new DataContext 
    { 
     // ThatAreIn <- this is where I am stuck 
     var items = _Db.Items.ThatAreIn(idsToValidate); 
     _Db.Items.DeleteAllOnSubmit(items); 
     _Db.SubmitChanges(); 
    } // Disposes of DataContext 
} 

내가이 데이터베이스에 하나 개의 여행에서 수행받을 수 여기에 내가 쓰고 싶은 것이 샘플입니다? 그렇다면 어떻게? 모든 int를 데이터베이스에 매개 변수 목록으로 보낼 수 있습니까? 목록에서 foreach를 수행하는 것보다 한 번에 하나씩 각 항목을 선택하는 것이 더 효율적입니까?

답변

3

당신은 할 수 있습니다 :

var items = _Db.Items.Where(i => idsToValidate.Contains(i.Key)); 

그 직장이나 내가 놓친 거지 무언가를?

+0

알고있었습니다. flipdoubt

관련 문제