【问题标题】:Linq Join query 4 tablesLinq Join 查询4张表
【发布时间】:2017-12-23 09:54:41
【问题描述】:

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

谢谢

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

【问题讨论】:

  • 提示:使用foreach 循环。
  • 但是怎么做呢?对于每个问题都必须创建一个新变量,例如 question1、question2 等,并且答案相同。
  • 这取决于您的查询是否与您调用它的范围相同,无论您是否应该查看此答案here

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


【解决方案1】:

您需要像这样枚举查询的结果

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 中嵌套一个列表或 stacklayout,并将数据源设置为 Answers 属性。

【讨论】:

  • 感谢您的帮助,但给了我相同的输出。
  • Phill,db 是如何定义的——是实体框架还是自定义类?
  • 如果我在 LinqPad 中模拟这个确切的查询,它会按预期工作,所以在 db 上下文的范围内可能存在问题?您可以尝试将此查询包装在 using(connectionname context = new connectionname) {...} 中,或使用 LinqPad 之类的东西将您的查询与其余代码隔离开来。
  • 我已经安装了 LinqPad5,我该如何测试呢?
  • 我添加了一个连接,然后我选择了默认(LINQ to SQL),然后是下一步,最后确定了。现在我可以看到我的数据库,我必须右键单击数据库然后新查询?
猜你喜欢
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多