在VB中,以下是一個有效的對象初始值設定項,其中一個成員初始化程序引用先前已初始化的另一個成員的值。如何引用C#成員初始值設定項中的另一個屬性值?
新的MyObject()含{.Property1 = 「X」,.Property2 = .Property1 + 「Y」}
如果嘗試使用做同樣在C#
新的MyObject(){ Property1 =「x」,Property2 = Property1 +「y」}
我得到錯誤名稱'Property1'在當前上下文中不存在。這有點令人驚訝,因爲兩種語言之間有相當的平衡,但也許這是少數差異之一。
有沒有辦法在C#中做到這一點?對於那些想知道具體用例可能是什麼的人,我正在使用相當複雜的LINQ查詢構造一個複合對象結構。
IEnumerable<Question> query =
(from q in dtQuestions.AsEnumerable()
join a in dtAnswers.AsEnumerable() on q.Field<int>("question_type_id") equals a.Field<int>("question_type_id") into Group
select new Question()
{
Id = q.Field<int>("question_type_id"),
SequenceNumber = q.Field<int>("sequence_no"),
IsChild = q.Field<bool>("isChildQuestion"),
EktronContentKey = q.Field<string>("ektron_content_key"),
Text = q.Field<string>("description"),
QuestionKindId = q.Field<int>("question_kind_type_id"),
Answers = (from a2 in Group
select new Answer()
{
Id = a2.Field<int>("answer_type_id"),
SequenceNumber = a2.Field<int>("sequence_no"),
EktronContentKey = a2.Field<string>("ektron_content_key"),
Text = a2.Field<string>("description"),
IsSelected = a2.Field<bool>("isSelected"),
ImageKey = q.Field<int>("question_type_id") == 2 ? "" : (Id % 2 == 0 ? "heating-gas-modern.png" : "heating-gas-condensing.png"),
ChildQuestionIds =
(from r in dtAnswerChildQuestions.AsEnumerable()
where r.Field<int>("answer_type_id") == Id
select r.Field<int>("question_type_id")).ToArray()
}).ToArray(),
SelectedAnswerId = QuestionKindId == 1 ?
(from Answer a3 in Answers
where a3.IsSelected == true
select a3.Id).SingleOrDefault() :
0,
SelectedAnswerIds = QuestionKindId == 2 ?
(from Answer a4 in Answers
where a4.IsSelected == true
select a4.id).ToArray() :
new int() { }
}
);
真正的問題在此,以解決是參照用於將值分配給SelectedAnswerId和SelectedAnswerIds的LINQ表達的答案屬性。我可能不得不將這兩個表達式分解爲他們自己的獨立作業。
我忽略了想Linq'Let'子句,所以OmegaMan有一個更乾淨的解決方案。 – MCattle