使用不同的数据组合填充网格的数据源

时间:2016-11-17 18:37:08

标签: c# algorithm


我有一个关于填充网格的问题。   
奥基,   
想象一下,我们有一个网格可以打印一些关于篮球队的数据。   
我们有四个主要类别:

public class Player
{
    public string PlayerName { get; set; }
}

public class Staff
{
    public string StaffName { get; set; }
}

public class Fan
{
    public string FanName { get; set; }
    public List<string> FanFavoriteTeam { get; set; }
}

public class Team
{
    public string TeamName { get; set; }
    public List<Player> Players { get; set; }
    public List<Staff> Staff { get; set; }
    public List<Fan> Fans { get; set; }
}

......当然还有填充网格的课程

public class Result
{
    public string TeamName { get; set; }
    public string PlayerName { get; set; }
    public string StaffName { get; set; }
    public string FanName { get; set; }
    public string FanFavoriteTeam { get; set; }
}

我想打印所有可能的组合,例如假设我们有这些数据:     
团队名称 - 金州勇士队     
球员 - 斯蒂芬库里,凯文杜兰特     
工作人员 - 史蒂夫科尔,阿尔文金特里,罗恩亚当斯     
球迷 - Duke Allard(最爱:凯尔特人队,湖人队),Dorian Macy,Lamont Collingwood

网格应打印此信息:

1st row)Golden State - Stephen Curry - Steve Kerr - Duke Allard - Celtics
2)Golden State - Stephen Curry - Steve Kerr - Duke Allard - Lakers
3)Golden State - Stephen Curry - Steve Kerr - Dorian Macy
4)Golden State - Stephen Curry - Steve Kerr - Lamont Collingwood
5)Golden State - Stephen Curry - Alvin Gentry - Duke Allard - Celtics
6)Golden State - Stephen Curry - Alvin Gentry - Duke Allard - Lakers
7)Golden State - Stephen Curry - Alvin Gentry - Dorian Macy
8)Golden State - Stephen Curry - Alvin Gentry - Lamont Collingwood
9)Golden State - Stephen Curry - Ron Adams - Duke Allard - Celtics
10)Golden State - Stephen Curry - Ron Adams - Duke Allard - Lakers
11)Golden State - Stephen Curry - Ron Adams - Dorian Macy
12)Golden State - Stephen Curry - Ron Adams - Lamont Collingwood
13)Golden State - Kevin Durant - Steve Kerr - Duke Allard - Celtics
14)Golden State - Kevin Durant - Steve Kerr - Duke Allard - Lakers
15)Golden State - Kevin Durant - Steve Kerr - Dorian Macy
16)Golden State - Kevin Durant - Steve Kerr - Lamont Collingwood
17)Golden State - Kevin Durant - Alvin Gentry - Duke Allard - Celtics
18)Golden State - Kevin Durant - Alvin Gentry - Duke Allard - Lakers
19)Golden State - Kevin Durant - Alvin Gentry - Dorian Macy
20)Golden State - Kevin Durant - Alvin Gentry - Lamont Collingwood
21)Golden State - Kevin Durant - Ron Adams - Duke Allard - Celtics
22)Golden State - Kevin Durant - Ron Adams - Duke Allard - Lakers
23)Golden State - Kevin Durant - Ron Adams - Dorian Macy
24)Golden State - Kevin Durant - Ron Adams - Lamont Collingwood

P.S。我希望在没有链接的foreach语句的情况下实现这一点
P.P.S.很抱歉这个长篇描述

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ为您执行循环:

var result = from t in teams
             from p in players
             from s in staff
             from f in fans
             select new Result {
               TeamName = t.TeamName,
               PlayerName = p.PlayerName,
               StaffName = s.StaffName,
               FanName = f.FanName,
             }

这基本上是表的交叉连接,没有任何限制,所以每个团队都与每个玩家,员工和粉丝匹配,我们得到Cartesian product数据。