考虑以下因素:
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
// select lastName from peopleArray where firstName like '%'J'%'
}
}
Run Code Online (Sandbox Code Playgroud)
使用LINQ,如何表达:
select lastName from peopleArray where firstName like '%'J'%'
Run Code Online (Sandbox Code Playgroud)
我想打印lastnames所有在他们身上有"J"的人firstname.我发现很难在LINQ中表达它.请帮忙....
var query = from person in peopleArray
where person.firstName.Contains("J")
select person.lastName;
// or
var query = peopleArray.Where(p => p.firstName.Contains("J")).Select(p => p.lastName);
// use results, print to screen?
foreach (string lastName in query)
{
Console.WriteLine(lastName);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
129 次 |
| 最近记录: |