Alan2
Alan2

Reputation: 24572

Can I combine two Linq calls when they use different syntax?

I have the following code. The function has a lot of Linq calls and I had help on putting this into place.

    public IList<Content.Grid> Details(string pk)
    {
        IEnumerable<Content.Grid> details = null;
        IList<Content.Grid> detailsList = null;
        var data = _contentRepository.GetPk(pk);
        var refType = this.GetRefType(pk);
        var refStat = this.GetRefStat(pk);
        var type = _referenceRepository.GetPk(refType);
        var stat = _referenceRepository.GetPk(refStat);
        details =
        from d in data
        join s in stat on d.Status equals s.RowKey into statuses
        from s in statuses.DefaultIfEmpty()
        join t in type on d.Type equals t.RowKey into types
        from t in types.DefaultIfEmpty()
        select new Content.Grid
        {
            PartitionKey = d.PartitionKey,
            RowKey = d.RowKey,
            Order = d.Order,
            Title = d.Title,
            Status = s == null ? null : s.Value,
            StatusKey = d.Status,
            Type = t == null ? null : t.Value,
            TypeKey = d.Type,
            Link = d.Link,
            Notes = d.Notes,
            TextLength = d.TextLength
        };
        detailsList = details
                .OrderBy(item => item.Order)
                .ThenBy(item => item.Title)
                .Select((t, index) => new Content.Grid()
                {
                    PartitionKey = t.PartitionKey,
                    RowKey = t.RowKey,
                    Row = index + 1,
                    Order = t.Order,
                    Title = t.Title,
                    Status = t.Status,
                    StatusKey = t.StatusKey,
                    Type = t.Type,
                    TypeKey = t.TypeKey,
                    Link = t.Link,
                    Notes = t.Notes,
                    TextLength = t.TextLength,

                })
                 .ToList();
        return detailsList;

    }

The first uses one format for Linq and the second another. Is there some way that I could simplify and/or combine these? I would really like to make this code simpler but I am not sure how to do this. Any suggestions would be much appreciated.

Upvotes: 1

Views: 113

Answers (1)

Botz3000
Botz3000

Reputation: 39610

Of course you can combine them. The Linq keywords such as from, where and select get translated into calls like the Extension methods that you call below, so effectively there's no difference.

If you really want to combine them, the quickest way is to put () around the first query, then append the method calls you use on details in the second query. Like this:

detailsList =
        (from d in data                    // <-- The first query
         // ...
         select new Content.Grid
         {
             // ...
         })
         .OrderBy(item => item.Order)    // <-- The calls from the second query
         .ThenBy(item => item.Title)
         .Select((t, index) => new Content.Grid()
         {
             //...
         }).ToList();

But i think that would be ugly. Two queries are just fine IMO.

Upvotes: 1

Related Questions