将对象从 Linq 中的列表获取到新列表
Get objects from list in Linq to new list
我正在尝试测试控件中是否存在问题,然后选择要添加到列表中的问题文本。
我有 20 个问题的清单。屏幕上只有 7 个可见。我想从列表中获取与项目值对应的对象
public partial class CustomQuestion
{
public string QuestionID { get; set; }
public string Question { get; set; }
public string ParentQuestionID { get; set; }
public int QuestionOrder { get; set; }
public string ShowOn { get; set; }
public string Option0 { get; set; }
public string Option1 { get; set; }
public string SelectedOption;
}
public partial class MultipleChoiceQuestion : UserControl
{
public string Answer { get; set; }
public string Question { get; set; }
}
public partial class Form1
{
private List<CustomQuestion> MyQuestion = new List<CustomQuestion>();
private void FindObjects()
{
var mylist = MyQuestion.
Where(qq => qq.Question == FlowLayouPanel1.Controls.Cast<Control>().
Where(x => x is MultipleChoiceQuestion).Cast<MultipleChoiceQuestion>().
Select(c => c.Question));
}
}
我得到的错误是:
Error 1 Operator '==' cannot be applied to operands of type 'string' and 'System.Collections.Generic.IEnumerable<string>
我希望这足以说明我正在尝试做什么。如果您有任何问题或要点我可以澄清,请告诉我。
你需要这样写:
var mylist = MyQuestion.
Where(qq => FlowLayouPanel1.Controls.OfType<MultipleChoiceQuestion>().
Select(c => c.Question).Any(c => qq.Question == c));
然后它将 return 所有存在于 MyQuestion
和
中的问题
FlowLayouPanel1.Controls
.OfType<MultipleChoiceQuestion>()
.Select(c => c.Question)
但这可能取决于你想要达到的目标
您的原始代码的问题在于您试图比较 string
类型的 qq.Question
与 return 由正确表达式编辑的 List<string>
。但我想你需要检查右表达式是否包含左字符串,所以我的答案是如何制作它的示例
您正在尝试将单个问题与一组问题进行比较。你需要做一个交叉点。
为避免在控件中重新构建问题列表 20 次,请首先使用控件中的问题填充列表:
var controlQuestions = FlowLayouPanel1.Controls
.OfType<MultipleChoiceQuestion>()
.Select(c => c.Question)
.ToList();
然后做交集:
var mylist = MyQuestion.Where(q => controlQuestions.Contains(q));
我正在尝试测试控件中是否存在问题,然后选择要添加到列表中的问题文本。
我有 20 个问题的清单。屏幕上只有 7 个可见。我想从列表中获取与项目值对应的对象
public partial class CustomQuestion
{
public string QuestionID { get; set; }
public string Question { get; set; }
public string ParentQuestionID { get; set; }
public int QuestionOrder { get; set; }
public string ShowOn { get; set; }
public string Option0 { get; set; }
public string Option1 { get; set; }
public string SelectedOption;
}
public partial class MultipleChoiceQuestion : UserControl
{
public string Answer { get; set; }
public string Question { get; set; }
}
public partial class Form1
{
private List<CustomQuestion> MyQuestion = new List<CustomQuestion>();
private void FindObjects()
{
var mylist = MyQuestion.
Where(qq => qq.Question == FlowLayouPanel1.Controls.Cast<Control>().
Where(x => x is MultipleChoiceQuestion).Cast<MultipleChoiceQuestion>().
Select(c => c.Question));
}
}
我得到的错误是:
Error 1 Operator '==' cannot be applied to operands of type 'string' and 'System.Collections.Generic.IEnumerable<string>
我希望这足以说明我正在尝试做什么。如果您有任何问题或要点我可以澄清,请告诉我。
你需要这样写:
var mylist = MyQuestion.
Where(qq => FlowLayouPanel1.Controls.OfType<MultipleChoiceQuestion>().
Select(c => c.Question).Any(c => qq.Question == c));
然后它将 return 所有存在于 MyQuestion
和
FlowLayouPanel1.Controls
.OfType<MultipleChoiceQuestion>()
.Select(c => c.Question)
但这可能取决于你想要达到的目标
您的原始代码的问题在于您试图比较 string
类型的 qq.Question
与 return 由正确表达式编辑的 List<string>
。但我想你需要检查右表达式是否包含左字符串,所以我的答案是如何制作它的示例
您正在尝试将单个问题与一组问题进行比较。你需要做一个交叉点。
为避免在控件中重新构建问题列表 20 次,请首先使用控件中的问题填充列表:
var controlQuestions = FlowLayouPanel1.Controls
.OfType<MultipleChoiceQuestion>()
.Select(c => c.Question)
.ToList();
然后做交集:
var mylist = MyQuestion.Where(q => controlQuestions.Contains(q));