我有一个简单的模型
public class OneModel
{
public string Email { get; set; }
public string Name { get; set; }
}
我创建了一个简单的列表(OneModel数据类型):
List<OneModel> someLists = new List<OneModel>{
new OneModel{Name="A 1", Email="a1@some.com"},
new OneModel{Name="A 2", Email="a2@some.com"},
new OneModel{Name="A 3", Email="a3@some.com"},
new OneModel{Name="B 1", Email="b1@some.com"},
new OneModel{Name="C 1", Email="c1@some.com"},
}
我想从第一个列表中创建另一个带有随机连接Name
的列表,如:
List<string> ... {"A 1-A 3", "A 2-C 1",...}
或类似的东西。
怎么做?
答案 0 :(得分:3)
假设.NET Framework 4.0:
var random = new Random();
var result = new List<string>();
var count = random.Next(10, 20); // How many results do you want, really!?
for (int i = 0; i < count; ++i) {
var query = from item in someLists
order by random.Next()
select item.Name;
var items = query.Take(2); // I assume you want two "names" in each item
result.Add(string.Join("-", items));
}
检查result
随机随机随机列表...
但是你需要什么......?