C# Linq 根据对象属性从列表中选择对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13230468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq select object from list depending on objects attribute
提问by Wesley Skeen
I have a list of objects
我有一个对象列表
class Answer
{
bool correct;
}
List<Answer> Answers = new List<Answer>();
Is there a way in linq for me to select an object depending on its attribute?
在 linq 中有没有办法让我根据其属性选择一个对象?
So far I have
到目前为止我有
Answer answer = Answers.Single(a => a == a.Correct);
But it does not work
但它不起作用
采纳答案by Groo
First, Singlethrows an exception if there is more than one element satisfying the criteria. Second, your criteria should only check if the Correctproperty is true. Right now, you are checking if ais equal to a.Correct(which will not even compile).
首先,Single如果有多个元素满足条件,则抛出异常。其次,您的标准应该只检查Correct属性是否为true。现在,您正在检查是否a等于a.Correct(甚至不会编译)。
You should use First(which will throw if there are no such elements), or FirstOrDefault(which will return nullfor a reference type if there isn't such element):
您应该使用First(如果没有这样的元素,它将抛出),或者FirstOrDefault(如果没有这样的元素,它将返回null一个引用类型):
// this will return the first correct answer,
// or throw an exception if there are no correct answers
var correct = answers.First(a => a.Correct);
// this will return the first correct answer,
// or null if there are no correct answers
var correct = answers.FirstOrDefault(a => a.Correct);
// this will return a list containing all answers which are correct,
// or an empty list if there are no correct answers
var allCorrect = answers.Where(a => a.Correct).ToList();
回答by Asif Mushtaq
if a.Correctis a boolflag for the correct answer then you need.
如果a.Correct是bool正确答案的标志,那么您需要。
Answer answer = Answers.Single(a => a.Correct);
回答by Habib
I assume you are getting an exception because of Single. Your list may have more than one answer marked as correct, that is why Singlewill throw an exception use First, or FirstOrDefault();
我假设你因为Single得到了一个例外。您的列表可能有多个标记为正确的答案,这就是为什么Single使用First或FirstOrDefault()会抛出异常的原因;
Answer answer = Answers.FirstOrDefault(a => a.Correct);
Also if you want to get list of all items marked as correct you may try:
此外,如果您想获取标记为正确的所有项目的列表,您可以尝试:
List<Answer> correctedAnswers = Answers.Where(a => a.Correct).ToList();
If your desired result is Single, then the mistake you are doing in your query is comparing an item with the bool value. Your comparison
如果您想要的结果是Single,那么您在查询中犯的错误是将一个项目与 bool 值进行比较。你的比较
a == a.Correct
is wrong in the statement. Your single query should be:
声明中是错误的。您的单个查询应该是:
Answer answer = Answers.Single(a => a.Correct == true);
Or shortly as:
或简称为:
Answer answer = Answers.Single(a => a.Correct);
回答by Jonas W
I think you are looking for this?
我想你在找这个?
var correctAnswer = Answers.First(a => a.Correct);
You can use single by typing :
您可以通过键入来使用 single :
var correctAnswer = Answers.Single(a => a.Correct);
回答by Dave New
Of course!
当然!
Use FirstOrDefault()to select the first object which matches the condition:
使用FirstOrDefault()选择哪个条件一致的第一个对象:
Answer answer = Answers.FirstOrDefault(a => a.Correct);
Otherwise use Where()to select a subset of your list:
否则用于Where()选择列表的子集:
var answers = Answers.Where(a => a.Correct);
回答by Paul Alan Taylor
Your expression is never going to evaluate.
你的表达永远不会评估。
You are comparing awith a property of a.
您正在a与 的属性进行比较a。
ais of type Answer. a.Correct, I'm guessing is a boolean.
a是类型答案。 a.Correct,我猜是一个布尔值。
Long form:-
长表:-
Answer = answer.SingleOrDefault(a => a.Correct == true);
Short form:-
简写:-
Answer = answer.SingleOrDefault(a => a.Correct);
回答by Yahya
Few things to fix here:
这里需要解决的几件事:
- No parenthesis in class declaration
- Make the "correct" property as public
- And then do the selection
- 类声明中没有括号
- 将“正确”的属性设为公开
- 然后做选择
Your code will look something like this
你的代码看起来像这样
List<Answer> answers = new List<Answer>();
/* test
answers.Add(new Answer() { correct = false });
answers.Add(new Answer() { correct = true });
answers.Add(new Answer() { correct = false });
*/
Answer answer = answers.Single(a => a.correct == true);
and the class
和班级
class Answer
{
public bool correct;
}
回答by Dweth
Answers = Answers.GroupBy(a => a.id).Select(x => x.First());
This will select each unique object by email
这将通过电子邮件选择每个唯一的对象

