我有一个员工列表,我想构建一个字符串,其中包含逗号分隔所有LastNames。
样本数据:
Emp1.LastName = "A"
Emp2.LastName = "B"
Emp3.LastName = "C"
Dim listing As List(Of Employee)
dim flatted as string
最后我想得到
flatted = "A,B,C"
重点是我想使用 linq (vb或c#)来实现
答案 0 :(得分:1)
这是如何在C#中完成的。我使用的是一个稍微不同的类(Person,而不是Employee),但代码最终会相同。
// Make some experimental data...
List<Person> peeps = new List<Person>()
{
new Person() { FirstName = "Frank", LastName = "Jax" },
new Person() { FirstName = "Anne", LastName = "Wax" },
};
// This will select all of the last names from the list of people, and join them with commas.
string lastNames = string.Join(",", (from x in peeps select x.LastName));
和班级列表,为好奇。
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
答案 1 :(得分:1)
以下内容应该有效:
List<Person> people = new List<Person>();
people.Add(new Person() { LastName = "A" });
people.Add(new Person() { LastName = "B" });
people.Add(new Person() { LastName = "C" });
var lastNames = (from person in people
select person.LastName);
var result = string.Join(",", lastNames);
Console.WriteLine(result);