Linq加入查询4个表

时间:2017-07-18 13:33:55

标签: asp.net sql-server linq xamarin entity-framework-6

我正在尝试加入下面的四个表格,下面的查询,但我不知道如何检索与每个调查问卷相关的所有答案和问题,我该怎么做? 使用此查询仅检索一个答案和问题。

由于

var query = from x in db.Questionario
                        join y in db.Question on x.QuestionarioId equals y.QuestionarioId 
                        join j in db.Answer on y.QuestionId equals  j.QuestionId
                        join w in db.TypeAnswer on y.TypeAnswerId equals w.TypeAnswerId

                    select new QuestionarioAlldetails
            {

                TypesAnswer = w.TypesAnswer,
                Questiontext = y.Questiontext,
                Answerv=j.Answerv,
                Name=x.Name,

             };

我想使用查询输出:

<ContentPage.BindingContext>
    <viewModels:InqueritogViewModel/>
</ContentPage.BindingContext>

<StackLayout>
    <Button Command="{Binding GetinqueritoCommand}" Text="Aceder a Questionarios"></Button>

    <ListView x:Name="InqueritoView" ItemsSource="{Binding Inqueritos}" HasUnevenRows="True" ItemSelected="ListView_OnItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <StackLayout >
                            <Label x:Name="Label1" Text="{Binding Questiontext}"></Label>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal">
                            <controls:Checkbox></controls:Checkbox>
                            <Label Text="{Binding Answerv}" VerticalTextAlignment="Center"></Label>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal">
                            <controls:Checkbox></controls:Checkbox>
                            <Label Text="{Binding Answerv}" VerticalTextAlignment="Center"></Label>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal" >
                            <controls:Checkbox></controls:Checkbox>
                            <Label Text="{Binding Answerv}" VerticalTextAlignment="Center"></Label>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</StackLayout>

Tables data

Output Query

1 个答案:

答案 0 :(得分:0)

您需要枚举查询中的结果,例如

List<QuestionarioAlldetails> query = (from x in db.Questionario
                    join y in db.Question on x.QuestionarioId equals y.QuestionarioId 
                    join j in db.Answer on y.QuestionId equals  j.QuestionId
                    join w in db.TypeAnswer on y.TypeAnswerId equals w.TypeAnswerId

                select new QuestionarioAlldetails
        {

            TypesAnswer = w.TypesAnswer,
            Questiontext = y.Questiontext,
            Answerv=j.Answerv,
            Name=x.Name,

         }).ToList();

修改

要为每个问卷创建一个包含多个答案选项的单行,请将结果嵌套为这样的结果;

List<Questionarios> query = (from x in db.Questionario
                select new Questionarios
        {
            Name = x.Name,
            Answers = (from y in db.Question  
                    join j in db.Answer on y.QuestionId equals  j.QuestionId
                    join w in db.TypeAnswer on y.TypeAnswerId equals w.TypeAnswerId
                    where y.QuestionarioId == x.QuestionarioId
                    select new Answer { 

                    TypesAnswer = w.TypesAnswer,
                   Questiontext = y.Questiontext,
                   Answerv=j.Answerv}).ToList()

                }).ToList();

(您需要使用Questionarios类来获取List属性来保存答案详细信息)

然后,您可以在主堆栈布局中嵌套列表或stacklayout,并将数据源设置为Answers属性。