문제

간단한 문제를 처리하는 가장 좋은 방법을 찾으려고 노력하고 있습니다. 두 테이블에 간단한 LINQ 가입이 있습니다. 생성 된 DBML 클래스와 동일하기 때문에 한 테이블의 유형을 반환하는 방법을 알고 있습니다. 그러나 두 테이블에서 데이터를 반환하려면 어떻게해야합니까? 둘 다 반환하고 관계를 사용할 수있는 방법이 없습니까? 두 테이블에서 데이터를 반환하려면 다른 반환 유형을 만들어야합니까? 참고- 다른 테이블 객체와 함께 출력 매개 변수를 반환하고 싶지 않습니다. 또한 익명 유형을 반환하는 데 관심이 없습니다. 모범 사례 권장 사항은 무엇입니까?

    public IQueryable<Consumer_Question> GetQuestions(int subCategoryId)
    {
        //create DataContext
        MototoolsDataContext mototoolsDataContext = new MototoolsDataContext();
        mototoolsDataContext.Log = Console.Out;

        var subcategoriestag = (from subCatTag in mototoolsDataContext.Consumer_SubCategoriesTags
                                join tagQuestion in mototoolsDataContext.Consumer_TagQuestions on subCatTag.TagID equals tagQuestion.TagID
                                join question in mototoolsDataContext.Consumer_Questions on tagQuestion.QuestionsID equals question.ID
                                where subCatTag.SubCategoriesID == subCategoryId
                                orderby subCatTag.ID descending
                                select question);
                                //select new { question, tagQuestion });

        return subcategoriestag;
    }

도움을 주셔서 감사합니다.

도움이 되었습니까?

해결책

LINQ-to-SQL 디자이너에서 관계를 정의한 경우 위의 쿼리는 조인 구문이 전혀 필요하지 않으며 필요에 따라 '트리를 걸어'하십시오.

var subCategoriesTag = (
    from subCatTag in motoToolsDataContext
    from tagQuestion in subCatTag.TagQuestions
    from question in tagQuestion
    where subCatTag.SubCategoriesID == subcategoryId
    orderby subCatTag.ID descending
    select question
);

LINQ-to-SQL은 이미 관계에 대해 알아야하기 때문에 이전의 객체의 객체를 사용하고 있습니다.

당신의 관계에 대해 더 많이 알지 못하면 더 정확한 답변을 제공하기가 더 어렵습니다. 나는 관련 속성이 무엇인지에 대해 몇 가지 가정을해야했다.

다른 팁

당신이 찾고있는 것이 DataLoadOptions.Loadwith <>입니다. 이렇게하면 질문 객체를 반환하고 관련 객체는 정의 된 연관성을 통해 동시에 채워집니다. 이 같은:

public IQueryable<Consumer_Question> GetQuestions(int subCategoryId)
{
    //create DataContext
    using (MototoolsDataContext mototoolsDataContext = new MototoolsDataContext())
    {
        mototoolsDataContext.Log = Console.Out;
        DataLoadOptions options = new DataLoadOptions();
        options.LoadWith<Consumer_Questions>(q => q.Consumer_TagQuestions);
        options.LoadWith<Consumer_TagQuestions>(tag => tag.Consumer_SubCategoriesTags);
        mototoolsDataContext.LoadOptions = options;

        var questions = (from subCatTag in mototoolsDataContext.Consumer_SubCategoriesTags
                                join tagQuestion in mototoolsDataContext.Consumer_TagQuestions on subCatTag.TagID equals tagQuestion.TagID
                                join question in mototoolsDataContext.Consumer_Questions on tagQuestion.QuestionsID equals question.ID
                                where subCatTag.SubCategoriesID == subCategoryId
                                orderby subCatTag.ID descending
                                select question);
                                //select new { question, tagQuestion });

        return questions;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top