Linq to SQL:マルチテーブル結合の戻り値の型はdbmlによって生成されません

StackOverflow https://stackoverflow.com/questions/1435517

質問

私は単純な問題を処理するための最良の方法を見つけようとしています。 2つのテーブルへの簡単なLINQ結合があります。生成されたdbmlクラスと同じであるため、1つのテーブルの型を返す方法を知っています。しかし、両方のテーブルからデータを返したい場合はどうすればいいですか?両方を返し、それらの関係を使用する方法はありませんか?両方のテーブルからデータを返すために、本当に別の戻り型を作成する必要がありますか?参考までに、他のテーブルオブジェクトと共に出力パラメーターを返したくありません。また、匿名型を返すことにあまり興味がありません。ベストプラクティスの推奨事項は何ですか?

    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はすでに関係を知っている必要があるため、2番目と3番目の 'from'ステートメントは前のステートメントを使用していることに注意してください。

あなたの関係についてもっと知ることなく、より正確な答えを出すことは困難です。関連するプロパティが何であるかについて、いくつかの仮定をしなければなりませんでした。

他のヒント

探しているのはDataLoadOptions.LoadWith <!> lt; <!> gt;です。そのようにして、Questionオブジェクトを返し、定義された関連付けを介して関連オブジェクトに同時にデータが入力されます。このようなもの:

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