Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 要在WPF datagrid中显示的嵌套集合项_C#_Wpf_Linq_Collections_Datagrid - Fatal编程技术网

C# 要在WPF datagrid中显示的嵌套集合项

C# 要在WPF datagrid中显示的嵌套集合项,c#,wpf,linq,collections,datagrid,C#,Wpf,Linq,Collections,Datagrid,我需要显示集合(列表类型)中的子项(列表类型)以及WPF数据网格中的索引 下面是一个示例和输出,以更好地解释它 示例: public class Quote { public int Id { get; set; } public List<Rate> Rates { get; set; } } public class Rate { public int Id { get; set; } public string AccommodationTyp

我需要显示集合(列表类型)中的子项(列表类型)以及WPF数据网格中的索引

下面是一个示例和输出,以更好地解释它

示例:

public class Quote
{
    public int Id { get; set; }
    public List<Rate> Rates { get; set; }
}

public class Rate
{
    public int Id { get; set; }
    public string AccommodationType { get; set; }
    public decimal Price { get; set; }
}

public class Simple 
{
    public static void Main()
    {
      List<Quote> quotes = new List<Quote> 
      {
        new Quote 
        { 
            Id = 1, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 11, AccommodationType = "A", Price = 1},
                new Rate { Id = 12, AccommodationType = "B", Price = 2}
            } 
        },
        new Quote 
        { 
            Id = 2, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 21, AccommodationType = "C", Price = 3},
                new Rate { Id = 22, AccommodationType = "D", Price = 4},
                new Rate { Id = 23, AccommodationType = "E", Price = 5}
            } 
        },
          new Quote 
        { 
            Id = 3, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 31, AccommodationType = "F", Price = 6},
                new Rate { Id = 32, AccommodationType = "G", Price = 7},
                new Rate { Id = 33, AccommodationType = "H", Price = 8}
            } 
        }
      };
    }
}
| QuoteID | RateIndex | RateID | AccommodationType | Price |

| 1       | 0         | 11     | A                 | 1     |

| 1       | 1         | 12     | B                 | 2     |

| 2       | 0         | 21     | C                 | 3     |

| 2       | 1         | 22     | D                 | 4     |

| 2       | 2         | 23     | E                 | 5     |

| 3       | 0         | 31     | F                 | 6     |

| 3       | 1         | 32     | G                 | 7     |

| 3       | 2         | 33     | H                 | 8     |
注意:RateIndex只是一个报价中的索引。有索引可能听起来很奇怪,但这是我不能放弃的需求

我尝试过的:

public class Quote
{
    public int Id { get; set; }
    public List<Rate> Rates { get; set; }
}

public class Rate
{
    public int Id { get; set; }
    public string AccommodationType { get; set; }
    public decimal Price { get; set; }
}

public class Simple 
{
    public static void Main()
    {
      List<Quote> quotes = new List<Quote> 
      {
        new Quote 
        { 
            Id = 1, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 11, AccommodationType = "A", Price = 1},
                new Rate { Id = 12, AccommodationType = "B", Price = 2}
            } 
        },
        new Quote 
        { 
            Id = 2, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 21, AccommodationType = "C", Price = 3},
                new Rate { Id = 22, AccommodationType = "D", Price = 4},
                new Rate { Id = 23, AccommodationType = "E", Price = 5}
            } 
        },
          new Quote 
        { 
            Id = 3, 
            Rates = new List<Rate> 
            {
                new Rate { Id = 31, AccommodationType = "F", Price = 6},
                new Rate { Id = 32, AccommodationType = "G", Price = 7},
                new Rate { Id = 33, AccommodationType = "H", Price = 8}
            } 
        }
      };
    }
}
| QuoteID | RateIndex | RateID | AccommodationType | Price |

| 1       | 0         | 11     | A                 | 1     |

| 1       | 1         | 12     | B                 | 2     |

| 2       | 0         | 21     | C                 | 3     |

| 2       | 1         | 22     | D                 | 4     |

| 2       | 2         | 23     | E                 | 5     |

| 3       | 0         | 31     | F                 | 6     |

| 3       | 1         | 32     | G                 | 7     |

| 3       | 2         | 33     | H                 | 8     |
创建以下类的另一个集合:

public class FormattedCollection
{
    int quoteID;
    int rateIndex;
    Rate rate;
}
我通过迭代每个报价和每个费率来填充这个集合。 然后,此集合将成为datagrid的ItemsSource

我不确定,但有没有更好的方法来实现这一点?
可能使用LINQ或任何其他方法?

您可以使用

创建一个新类以合并报价和费率

public class QuoteRate
{
    private Quote _quote;
    private Rate _rate;

    public QuoteRate(Quote quote, Rate rate)
    {
        _quote = quote;
        _rate = rate;
    }

    public int QuoteID => _quote.Id;
    public int RateIndex => _quote.Rates.IndexOf(_rate);
    public int RateID => _rate.Id;
    public string AccommodationType => _rate.AccommodationType;
    public Decimal Price => _rate.Price;
}
然后展平quotes集合

var allQuoteRates = quotes.SelectMany(q => q.Rates, (q, r) => new QuoteRate(q, r)).ToList();
然后,您可以使用此扁平列表作为datagrid的ItemsSource

也可以使用,这样可以节省创建新类型的工作量

quotes.SelectMany(q => q.Rates, (q, r) => new { Quote = q, Rate = r, RateIndex = q.Rates.IndexOf(r) }).ToList();

尽管这使得datagrid的绑定更加复杂

,但我还是会尝试一下。它与我尝试过的一件事情类似,创建了一个新类,其集合可以用作源项。有没有不创建新类的方法?您也可以使用匿名类型-请参阅上面的编辑。