简体   繁体   English

有没有办法组合这两个LINQ语句?

[英]Is there a way I could combine these two LINQ statements?

I have the following: 我有以下内容:

var data = _cityRepository.GetAll(
    u => u.PartitionKey == pk & 
    u.RowKey.CompareTo(lowerBound) >= 0 & 
    u.RowKey.CompareTo(upperBound) < 0);

details =
    from d in data
    select new City.Grid
    {
        PartitionKey = d.PartitionKey,
        RowKey = d.RowKey,
        ShortTitle = d.ShortTitle,
        Created = d.Created,
        Modified = d.Modified,
        ModifiedBy = d.ModifiedBy
    };

detailsList = details
    .OrderBy(item => item.Modified)
    .Select((t, index) => new City.Grid()
    {
        PartitionKey = t.PartitionKey,
        RowKey = t.RowKey,
        Row = index + 1,
        ShortTitle = t.ShortTitle,
        Created = t.Created,
        Modified = t.Modified,
        ModifiedBy = t.ModifiedBy
    })
    .ToList();

The problem for me is that I am not sure how to combine the second as it uses this: 对我来说问题是我不知道如何结合使用第二个:

Select((t, index) => new City.Grid()

Is there a way I could combine these statements into one or if that's not possible could I just combine the last two? 有没有办法将这些陈述合并为一个,或者如果不可能,我可以将最后两个合并?

Sure - it's ugly, but this has all three: 当然 - 它很难看,但这有三个:

var query = _cityRepository.GetAll(
                               u => u.PartitionKey == pk & 
                               u.RowKey.CompareTo(lowerBound) >= 0 & 
                               u.RowKey.CompareTo(upperBound) < 0)
                .Select(d => new City.Grid
                        {
                            PartitionKey = d.PartitionKey,
                            RowKey = d.RowKey,
                            ShortTitle = d.ShortTitle,
                            Created = d.Created,
                            Modified = d.Modified,
                            ModifiedBy = d.ModifiedBy                          
                        }
                .OrderBy(item => item.Modified)
                .Select((t, index) => new City.Grid()
                {
                    PartitionKey = t.PartitionKey,
                    RowKey = t.RowKey,
                    Row = index + 1,
                    ShortTitle = t.ShortTitle,
                    Created = t.Created,
                    Modified = t.Modified,
                    ModifiedBy = t.ModifiedBy
                })
                .ToList();

It would be more sensible to avoid creating new City.Grid objects twice: 避免两次创建新的City.Grid对象会更明智:

var query = _cityRepository.GetAll(
                               u => u.PartitionKey == pk & 
                               u.RowKey.CompareTo(lowerBound) >= 0 & 
                               u.RowKey.CompareTo(upperBound) < 0)
                .OrderBy(item => item.Modified)
                .Select((t, index) => new City.Grid()
                {
                    PartitionKey = t.PartitionKey,
                    RowKey = t.RowKey,
                    Row = index + 1,
                    ShortTitle = t.ShortTitle,
                    Created = t.Created,
                    Modified = t.Modified,
                    ModifiedBy = t.ModifiedBy
                })
                .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM