如何在给定属于Value

时间:2016-09-15 00:07:49

标签: c# .net linq dictionary lambda

假设我有这本词典:

Dictionary<Guid, Person> people = new Dictionary<Guid, Person>();

假设它填充了一些数据。我们还要说Person有一个Ssn属性。我希望将Ssn = socialSecurityNum socialSecurityNum的人归还给public Person GetPerson(int socialSecurityNum) { IEnumerable<Person> persons = people.Values.Where(p => p.SSN == socialSecurityNum); foreach (Person person in persons) { return person; } return null; } 。这是我必须做的最好的主意:

Ssn

我知道每个人都会有不同的Sub Addpicture() Dim strPath Dim p As Object strPath = <String containing path here> Set p = ActiveSheet.Shapes.Addpicture(strPath, msoCTrue, msoTrue, 1000, 10, 86, 129) ActiveSheet.Pictures.Insert (strPath) End Sub ,所以我只返回列表中的第一个值(因为列表只是一个值)。否则返回null。

我不喜欢这个解决方案的事情是我必须从linq查询中获取一个列表而不是单个值。是否有可以从字典中获取单个值的linq查询?

1 个答案:

答案 0 :(得分:4)

而不是Where使用FirstOrDefault

return people.Values.FirstOrDefault(p => p.SSN == socialSecurityNum);
相关问题