Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编写实体框架查询的正确方法_C#_Entity Framework_Entity Framework 4_Linq To Entities - Fatal编程技术网

C# 编写实体框架查询的正确方法

C# 编写实体框架查询的正确方法,c#,entity-framework,entity-framework-4,linq-to-entities,C#,Entity Framework,Entity Framework 4,Linq To Entities,我想知道这样编写查询的正确方法: var questions = from q in db.Questions join sq in db.SurveyQuestions on q.QuestionID = sq.QuestionID where sq.SurveyID == 1 orderby sq.Order select q; 我基本上想从问号表中选择所有内容,

我想知道这样编写查询的正确方法:

var questions = from q in db.Questions
                join sq in db.SurveyQuestions on q.QuestionID = sq.QuestionID
                where sq.SurveyID == 1
                orderby sq.Order
                select q;
我基本上想从问号表中选择所有内容,它与另一个表中的值相匹配

我认为也可以这样编写查询:

var questions = from q in db.Questions
                from sq in q.SurveyQuestions
                where sq.SurveyID == 1
                orderby sq.Order
                select q;
这个查询不起作用,但更符合我的想法:

var questions = from q in db.Questions
                where q.SurveyQuestions.SurveyID == 1
                orderby q.SurveyQuestions.Order
                select q;

使用导航属性在实体框架中编写这些类型的查询的正确方法是什么?

还没有测试过这一点,但我认为这就是您要寻找的

var questions = from sq in db.SurveyQuestions
                where sq.SurveyID == 1
                orderby sq.Order
                select sq.Question;
其中
Question
SurveyQuestion
上的导航属性


您使用的是实体,而不是数据库表。这种类型的查询正是EF所关注的。您不必像在第一次查询中那样考虑数据库表。相反,您可以立即开始筛选
调查问题
,这更直观。导航属性将抽象掉如果您直接在数据库上操作的话可能会使用的连接。

还没有测试过这一点,但我假设这就是您正在寻找的

var questions = from sq in db.SurveyQuestions
                where sq.SurveyID == 1
                orderby sq.Order
                select sq.Question;
其中
Question
SurveyQuestion
上的导航属性


您使用的是实体,而不是数据库表。这种类型的查询正是EF所关注的。您不必像在第一次查询中那样考虑数据库表。相反,您可以立即开始筛选
调查问题
,这更直观。导航属性将抽象出如果您直接在数据库上操作,您将使用的联接。

是的,这可能更符合我要查找的内容。我会试试这个。是的,这可能更符合我的要求。我试试这个。