2012-02-23 5 views
3

나는 나이 또는 시간과 같은 여러 다 대다 관계가있는 모델 코스가 있습니다.여러 유사한 linq 쿼리

나는이 쿼리가 :

string IDs = "1,2,3" 
string[] IDList = IDs.Split(','); 

return (from x in entities.Course 
     where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count() 
     select x); 

을 그리고 난에로 시간과 몇 가지 다른 속성에 대해 동일한 쿼리를 설정해야합니다

string IDs = "1,2,3" 
string[] IDList = IDs.Split(','); 

return (from x in entities.Course 
     where x.Times.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count() 
     select x); 

가 어떻게 그래서 쿼리가 더 동적으로 만들 수 있습니다 비슷한 검색어가 여러 개 없습니까?

감사

+1

아니면이 엔티티에 Linq에 무엇입니까? 해결책은 크게 다를 것입니다. Linq의 경우 리팩터링을 메서드로 사용하기 만하면됩니다. Linq to Entities 솔루션은 http://www.albahari.com/nutshell/linqkit.aspx에 있습니다. –

답변

2

당신은 (데이터 유형에 depeneding)를 Expression을 받아들이는 방법을 만들 수 있고 그런 식으로 쿼리를 실행 : 당신도 연쇄 메소드 호출로 두 쿼리 필터를 결합 할 수 있습니다. Ages, Time 등을 작동시키기위한 특정 인터페이스를 구현해야합니다. 예를 들어

, 당신이 EF를 사용하고 모델 코드 먼저 DbSet의를 사용하고있는 가정이 만들 수 : 그것은 단지 Linq에

public interface IObject 
{ 
    int ID { get; set; } 
} 

public class Age : IObject 
{ 
    public int ID { get; set; } 

    // The rest of the data 
} 

public class Time : IObject 
{ 
    public int ID { get; set; } 

    // The rest of the data 
} 

public class Course 
{ 
    public virtual ICollection<Age> Ages { get; set; } 
    public virtual ICollection<Time> Times { get; set; } 
} 

public class CourseContext : DbContext 
{ 
    public DbSet<Course> Course { get; set; } 
} 

public class Test 
{ 
    public IQueryable<Course> GetCourses(Expression<Func<Course, ICollection<IObject>>> exp) 
    { 
     var entities = new CourseContext(); 
     string IDs = "1,2,3"; 
     string[] IDList = IDs.Split(','); 

     var c = exp.Compile(); 

     return entities.Course.Where(x => c.Invoke(x).Count(val => IDList.Contains(val.ID.ToString())) == IDList.Count()); 
    } 

    public void TestMethod() 
    { 
     var times = GetCourses(c => (ICollection<IObject>)c.Times); 
     var ages = GetCourses(c => (ICollection<IObject>)c.Ages); 
    } 
} 
+0

그 일을했다 - thanks – user441365

+0

No prob : 나는 표현을 좋아한다! 그들은 C#의 가장 좋아하는 측면 중 하나입니다. –

1

내가 다른 쿼리 결과를 반환하는 방법을 만들 것 - 단지 where 절은 다른

public IQuerable<Course> GetAllCourses() { 
    return entities.Course; 
} 

public IQueryable<Course> ByAge(IQueryable<Course> source, IEnumerable<String> ages { 
    return from x in source 
      where x.Ages.Where(val => ages.Contains(val.ID.ToString())).Count() == IDList.Count() 
      select x; 
} 

public IQuerable<Course> ByTimes(IQueryable<Course> source, IEnumerable<String> times) { 
    return from x in source 
      where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count() 
      select x; 
} 

이유는 방법 쿼리 로직을 캡슐화하는 것입니다. 그런 다음 모든 소스를 전달할 수 있습니다.

var ids = new [] { "1", "2", "3" }; 
var coursesByAgeAndTime = ByTime(ByAge(GetAllCourses(), ids), ids); 
+0

동일합니다. 동일합니다. 전체 쿼리를 1로 캡슐화하는 방법이 있는지 궁금합니다. – user441365

+0

@ user441365 이 메소드는'where' 로직을 캡슐화합니다. 'from x' 소스와'select x' 문장은'source.Where (...) '가 될 수 있습니다. – Yuck