0

A Cost is linked to an AnsweredQuestion in a PropertySurvey via QuestionID and AnswerID foreign keys. A Cost may also be linked to an AnsweredQuestion in the same PropertySurvey via the UnitsQuestionID and again via the ReplacementQuestionID. Here is the query as an inner join - written in a way that avoids the join syntax that @CraigStuntz regards as "messy"

var propertyCosts = from aq in answeredQuestions
    from a in aq.Answers
    from c in costs
    where aq.QuestionID == c.QuestionID && a.ID == c.AnswerID
    from uq in answeredQuestions
    where uq.QuestionID == c.UnitsQuestionID 
                        && uq.PropertySurveyID == aq.PropertySurveyID
    from rq in answeredQuestions
    where rq.QuestionID == c.ReplacementQuestionID 
                        && rq.PropertySurveyID == aq.PropertySurveyID
    select new PropertyCost(aq.Question.Text, 
    a.Text, 
    c.Amount, 
    uq.IntegerAnswer.GetValueOrDefault(1), 
    rq.IntegerAnswer.GetValueOrDefault(0));

Is it possible to rewrite this with left joins to UnitQuestion and ReplacementQuestion without that DefaultIfEmpty stuff?

4

2 回答 2

0

为了跟随 Craig Stuntz,你需要有你的逻辑可以遵循的关联。

因此,要对您的多对多建模,您将有一个中间表,该表具有与问题和答案的一对多关联。您可以在此处使用 Cost 并且免费获得零成本。

所以

Question(QuestionId) 1 -> 1..* Cost(QuestionId) Answer(AnswerId) 1 -> 1..* Cost(AnswerId) Question(QuestionId) 1 -> 0..* Cost(UnitQuestionId) Question(QuestionId) 1 -> 0..* 成本(替换问题 ID)

然后在您的模型中创建关联

Question (Property = Costs) -- (Property = Question) Costs
Question (no mapping) -- (Property = UnitQuestion) Costs
Question (no mapping) -- (Property = ReplacementQuestion) Costs
Answer (Property = Costs) -- (Property = Answer) Costs

此查询应与您的查询行为相匹配。

from aq in answeredQuestions
from c in aq.Costs
select new PropertyCost(aq.Question.Text, 
                        c.Answer.Text, 
                        c.Amount, 
                        c.UnitQuestion == null ? 0 : c.UnitQuestion.IntegerAnswer
                        c.ReplacementQuestion == null ? 0 : c.ReplacementQuestion.IntegerAnswer
于 2013-07-08T15:18:11.497 回答
0
//Make this problem less complicated to explain by abstracting the inner join 
//that we always need.
//For efficiency this should probably be put back in line
var A = from aq in answeredQuestions
        from a in aq.Answers
        from c in costs
        where aq.QuestionID == c.QuestionID && a.ID == c.AnswerID
        select new
        {
            QuestionText = aq.Question.Text,
            AnswerText = a.Text,
            Amount = c.Amount,
            PropertySurveyID = aq.PropertySurveyID,
            PropertySurvey = aq.PropertySurvey,
            UnitsQuestionID = c.UnitsQuestionID,
            UnitsQuestion = c.UnitsQuestion
        };


//This is how to do it using the explicit join keyword
var B = from a in A
        join lj in answeredQuestions on
            new { 
                   a.PropertySurveyID, 
                   UnitsQuestionID = a.UnitsQuestionID.GetValueOrDefault(0) 
                }
            equals new { 
                          lj.PropertySurveyID, 
                          UnitsQuestionID = lj.QuestionID 
                       }
            into unitQuestions
        from uq in unitQuestions.DefaultIfEmpty()
        select new PropertyCost(a.QuestionText,
                    a.AnswerText,
                    a.Amount,
                    uq == null ? 1 : uq.IntegerAnswer.GetValueOrDefault(1));

//I thought this might do it but the filter on the navigational property 
//means it's equivalent to an inner join
var C = from a in A
        from uq in a.PropertySurvey
                    .AnsweredQuestions
                    .Where(x => x.QuestionID == a.UnitsQuestionID)
        select new PropertyCost(a.QuestionText,
                    a.AnswerText,
                    a.Amount,
                    uq == null ? 1 : uq.IntegerAnswer.GetValueOrDefault(1));

//This is the solution
//Back to the basics described by @CraigStuntz
//Just that we have to navigate further to get to the Units value
//Is this less "messy" than a join? Not sure. Maybe if you can think in Linq...
var D = from a in A
        select new PropertyCost
        (
            a.QuestionText,
            a.AnswerText,
            a.Amount,
            a.PropertySurvey
                .AnsweredQuestions
                .Where(x => x.QuestionID == a.UnitsQuestionID)
                .FirstOrDefault() == null
            ?
            1
            : a.PropertySurvey
                .AnsweredQuestions
                .Where(x => x.QuestionID == a.UnitsQuestionID)
                .FirstOrDefault()
                .IntegerAnswer.GetValueOrDefault(1)
        );

//And here I have further refined it by putting the initial inner join back 
//inline and using the let keyword define how to retrieve the unit question. 
//This makes it much more readable:

var E = from aq in answeredQuestions
        from a in aq.Answers
        from c in costs
        let unitquestion = aq.PropertySurvey
            .AnsweredQuestions
            .Where(x => x.QuestionID == c.UnitsQuestionID)
            .FirstOrDefault()
        where aq.QuestionID == c.QuestionID && a.ID == c.AnswerID
        select new 
        {
            QuestionText = aq.Question.Text,
            AnswerText = a.Text,
            UnitCost = c.Amount,
            NumUnits = unitquestion == null ? 1 : unitquestion.IntegerAnswer ?? 1,
        };

我花了很长时间才得到这个。我仍然在 sql 中思考,我不禁想在实体框架中使用视图。请注意,我已对此进行了简化,仅显示 UnitQuestion 的第一个左连接

于 2013-07-09T14:05:03.993 回答