Jacked_Nerd
Jacked_Nerd

Reputation: 237

Issue with Linq query syntax. Cannot convert to list

I am getting an error when trying to convert my linq query to a list like I would normally do.

Here is my query:

var Services = (from sa in _ctx.ServiceAttrs
    join pp in _ctx.ProcessorProducts 
        on new { ServiceId = sa.ServiceID, PrsnPk = ActivePrsnPk } equals
        new { ServiceId = pp.ServiceID, PrsnPk = pp.PrsnPK } into tmp
            from PersonServices in tmp.DefaultIfEmpty()
                .Select(PersonServices => new ReviewerServiceDto()
                {
                    ServiceId = sa.ServiceID,
                    ServiceAliasDescription = sa.ServiceAlias,
                    IsSelected = (PersonServices.IsActivated == null) 
                        ? false
                        : true,
                }).OrderBy(dto => dto.ServiceAliasDescription).ToList();

I am getting redlined right at the ToList(). Tells me parenthesis can be removed, however when I remove them, it will no longer evoke the method to convert to list...

I thought I was missing a bracket somewhere but It looks good to me.

Upvotes: 0

Views: 86

Answers (2)

Martin Bonafede
Martin Bonafede

Reputation: 11

First of all, you are opening a bracket without closing it.

You are applying the OrderBy() and the ToList() to: tmp.DefaultIfEmpty().Select(..), if you want to do that on purpose, all you need is to add a select after that and close the bracket).

You need to add a select clause to tell what data you require from the query. This msdn article describes the basic query operation and structure.

  }).OrderBy(dto => dto.ServiceAliasDescription).ToList()   select something);

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112752

Besides a missing bracket, you are mixing LINQ syntax with extension method syntax. In from PersonServices in tmp.DefaultIfEmpty() .Select the select cannot be an extension method call.

This should work

var Services = (
    from sa in _ctx.ServiceAttrs
    join pp in _ctx.ProcessorProducts
        on new { ServiceId = sa.ServiceID, PrsnPk = ActivePrsnPk }
        equals new { ServiceId = pp.ServiceID, PrsnPk = pp.PrsnPK }
        into tmp
    from PersonServices in tmp.DefaultIfEmpty()
    select new ReviewerServiceDto() {
        ServiceId = sa.ServiceID,
        ServiceAliasDescription = sa.ServiceAlias,
        IsSelected = PersonServices.IsActivated != null
    }
)
.OrderBy(dto => dto.ServiceAliasDescription)
.ToList();

A proper indentation helps in distinguishing the LINQ syntax part from the extension method part.

Btw.: the second from would have to be expressed as SelectMany in extension method syntax.

Upvotes: 1

Related Questions