Samantha J T Star
Samantha J T Star

Reputation: 32758

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?

Upvotes: 1

Views: 79

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499870

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:

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();

Upvotes: 3

Related Questions