1
下面的查詢是不是從Visual Studio工作:的LINQ查詢不填充子集合
var query = this.ObjectContext.Questions.Include("AnswerKey").Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == 3));
但是在linqPad它像它應該使用:
Questions
.Include("AnswerKey")
.Where(o=>o.SurveyQuestions.Any(o2=>o2.SurveyID==3)).Dump();
查詢加載問題它應該加載,但它不會加載AnswerKeys的子集合。然而,linqpad查詢確實會返回子集合。經過一天的努力,我必須做些愚蠢的事情......請告訴我我的錯誤是什麼......謝謝。
添加更多細節: 我使用的EF和RIA服務在Silverlight 4.0應用
我從我的DomainServiceClass的部分類
public IQueryable<Data.Questions> GetQuestionsbySurveyId(int id)
{
//var query = this.ObjectContext.Questions.Include("AnswerKey").Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == id));
// var query = this.ObjectContext.Questions.Include("AnswerKey").Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == 3));
var query= this.ObjectContext.Questions.Include("AnswerKey").Include("SurveyQuestions");
//var query = this.ObjectContext.Questions;
Debug.WriteLine(((ObjectQuery)query).ToTraceString());
return query;
}
我不執行這個代碼知道這是否真的有什麼區別。 linqpad查詢使用與此相同的EF數據上下文。我已經驗證生成的SQL是相同的。在客戶端它會作爲EntityQuery返回嗎?
private void ReceiveNewQuestionairreRequest(fnReadmitPatientList_Result request)
{
CurrentSelectedPatient = request;
var context = new SurveysDomainContext();
EntityQuery<Questions> query = context.GetQuestionsbySurveyIdQuery(3);
context.Load(query, SurveyQuestions_Loaded, null);
//context.Load(q, AnswerKey_Loaded, null);
}
private void SurveyQuestions_Loaded(LoadOperation<Questions> lo)
{
if (!loHasError(lo))
{
QuestionsCollection = new ObservableCollection<Questions>(lo.Entities);
foreach (Questions q in QuestionsCollection)
{
Debug.WriteLine(String.Format("{0} {1} - Available Answers= {2}", q.ID, q.Text, q.AnswerKey.Count()));
}
}
}
在Linqpad這個作品,但從我的EF Datacontext它不包括返回... – ecathell 2011-03-01 19:10:07
愚蠢的RIA服務... bleh..it不是一個linq問題。我沒有在元數據中使用[包含] – ecathell 2011-03-01 20:51:18
@ecathell裝飾的AnswerKey集合 - 嘿,我認爲某些東西一定不能包含在某處。很高興你解決了你的問題。 :) – 2011-03-01 21:04:22