将在WPF数据网格中显示的嵌套收集项

时间:2018-11-06 12:11:34

标签: 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 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只是一个Quote中的索引。拥有索引可能听起来很奇怪,但这是我不能放弃的需要。

我尝试过的事情:

创建以下类别的另一个集合:

public class FormattedCollection
{
    int quoteID;
    int rateIndex;
    Rate rate;
}

我通过依次遍历每个报价和每个费率来填充此集合。 然后,此集合将成为数据网格的ItemsSource。

我不确定,但是有更好的方法吗? 也许使用LINQ或任何其他方法?

1 个答案:

答案 0 :(得分:0)

您可以使用Linq SelectMany()

整理报价清单

创建一个新的类,将报价和费率结合起来

autoIncrement = require('mongoose-auto-increment');
var UnknownSchema = new Schema({
    pictureType: {type: String},
    location: {type: String},
    leafname: {type: String},
    createduser: {type: String},
    lastedituser: {type: String},
    timestamp: { type: Date, default: Date.now},
});
UnknownSchema.plugin(autoIncrement.plugin,'UnknownSchema');

var addUnknown=mongoose.model('addUnknown', UnknownSchema);


//It is fetched like this
var getUnknown=function(req,res){
    addUnknown.find({},function (err,data) {
        if(err){
            res.send(err);
        }else {
            res.send(data);
        }
    })
};

然后扁平化报价集

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;
}

然后,您可以使用这个扁平化的列表作为数据网格的ItemsSource。

还可以使用anonymous type,从而省去了创建新类型的麻烦

var allQuoteRates = quotes.SelectMany(q => q.Rates, (q, r) => new QuoteRate(q, r)).ToList();

尽管这会使数据网格的绑定更加复杂