我的Linq查询(C#)有什么问题? [英] What is wrong with my Linq query (C#)?
本文介绍了我的Linq查询(C#)有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ViewModel;
I have a ViewModel;
public class GetQuestionViewModel
{
public IEnumerable<Question> Questions { get; set; }
public IEnumerable<QuestionOption> QuestionOptions { get; set; }
public int Id { get; set; }
public Nullable<int> Options { get; set; }
public string QuestionOption1 { get; set; }
public Nullable<int> QuestionOptionRanking { get; set; }
public string Question1 { get; set; }
public int QuestionTypeId { get; set; }
public Nullable<int> QuestionRanking { get; set; }
public string Answer { get; set; }
}
我的查询结构如此;
And I have my Query structured as thus;
[HttpGet]
public ActionResult ViewQuestion(int page = 1)
{
var userId = User.Identity.GetUserId();
ICollection<GetQuestionViewModel> question = (from q in db.Questions
join qo in db.QuestionOptions on q.Id equals qo.QuestionId
join r in db.Responses on q.Id equals r.QuestionId
where q.PageNumber == page
select new GetQuestionViewModel
{
QuestionOptionRanking = qo.QuestionOptionRanking,
Id = q.Id,
Options = q.Options,
QuestionTypeId = q.QuestionTypeId,
Question1 = q.Question1,
QuestionRanking = q.QuestionRanking,
Answer = r.Answer
}).ToList();
ViewBag.PageNumber = page;
return View(question);
}
我的关系是许多QuestionOptions的一个问题,但到目前为止我还没有找到正确的语法返回任何QuestionOptions,尽管所有其他字段都包含我期望的数据。任何人都可以看到我做错了吗?
My relationships is one Question to many QuestionOptions but as yet I have not found the correct syntax to return ANY of the QuestionOptions although all the other fields contain the data I expect. Can anyone see what I am doing wrong, please?
推荐答案
你可以用这个来模拟
问题列表 - 主要列表
QuestionsOptions列表 - 次要列表
休息列表 - 次要列表
例如:加入1(问题列表和问题选项列表) - >它会给2行
加入1个结果加入Resonse列表 - >给出4行作为父级(第二次连接)(连接1有2行)
You can simulate with this
Questions List - main list
QuestionsOptions list - secondary list
Respose List - secondary list
for example: join 1 ( question list and question option list ) -> it will give 2 rows
join 1 results joined with Resonse list -> gives 4 rows as the parent(for second join) ( join 1 has 2 rows )
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using console_poc;
namespace oops1
{
public class Db
{
public Db()
{
Questions = new List<Question>();
Questions.Add(new Question() { Id = 1, Options = 1, PageNumber = 1, Question1 = "q1", QuestionRanking = 1, QuestionTypeId = 1 });
QuestionOptions = new List<QuestionOption>();
QuestionOptions.Add(new QuestionOption() { QuestionId = 1, QuestionOptionRanking = 1 });
QuestionOptions.Add(new QuestionOption() { QuestionId = 1, QuestionOptionRanking = 2 });
QuestionOptions.Add(new QuestionOption() { QuestionId = 2, QuestionOptionRanking = 3 });
QuestionOptions.Add(new QuestionOption() { QuestionId = 2, QuestionOptionRanking = 4 });
Responses = new List<Response>();
Responses.Add(new Response() { Answer = "a1", QuestionId = 1 });
}
public List<QuestionOption> QuestionOptions { get; set; }
public List<Question> Questions { get; set; }
public List<Response> Responses { get; set; }
}
class Program
{
static void Main(string[] args)
{
int page = 1;
Db db = new Db();
// using lamda expression
//var result = db.Questions.Join(db.QuestionOptions, (k => k.Id), (k => k.QuestionId), (q, qo) => new { q, qo }).
// Join(db.Responses, (k => k.q.Id), (k => k.QuestionId), (qqo, r) => new { r, qqo }).Where(k=>k.qqo.q.PageNumber == page).Select(k =>
// new GetQuestionViewModel()
// {
// QuestionOptionRanking = k.qqo.qo.QuestionOptionRanking,
// Id = k.qqo.q.Id,
// Options = k.qqo.q.Options,
// QuestionTypeId = k.qqo.q.QuestionTypeId,
// Question1 = k.qqo.q.Question1,
// QuestionRanking = k.qqo.q.QuestionRanking,
// Answer = k.r.Answer
// }).ToList();
var question = (from q in db.Questions
join qo in db.QuestionOptions on q.Id equals qo.QuestionId
join r in db.Responses on q.Id equals r.QuestionId
where q.PageNumber == page
select new GetQuestionViewModel
{
QuestionOptionRanking = qo.QuestionOptionRanking,
Id = q.Id,
Options = q.Options,
QuestionTypeId = q.QuestionTypeId,
Question1 = q.Question1,
QuestionRanking = q.QuestionRanking,
Answer = r.Answer
}).ToList();
}
}
}
public class Question
{
public int Id { get; set; }
public int PageNumber { get; set; }
public Nullable<int> Options { get; set; }
public string Question1 { get; set; }
public int QuestionTypeId { get; set; }
public Nullable<int> QuestionRanking { get; set; }
}
public class QuestionOption
{
public int QuestionId { get; set; }
public Nullable<int> QuestionOptionRanking { get; set; }
}
public class Response
{
public int QuestionId { get; set; }
public string Answer { get; set; }
}
public class GetQuestionViewModel
{
public IEnumerable<Question> Questions { get; set; }
public IEnumerable<QuestionOption> QuestionOptions { get; set; }
public int Id { get; set; }
public Nullable<int> Options { get; set; }
public string QuestionOption1 { get; set; }
public Nullable<int> QuestionOptionRanking { get; set; }
public string Question1 { get; set; }
public int QuestionTypeId { get; set; }
public Nullable<int> QuestionRanking { get; set; }
public string Answer { get; set; }
}
这篇关于我的Linq查询(C#)有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文