0

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.

5
  • 1
    If I see it well, the first bracket does not have a closing pair (or can be remove that). Commented Jan 22, 2019 at 15:29
  • Is it redlined (error) or greenlined (warning) ? Post the exact message, including the error/warning number. Commented Jan 22, 2019 at 15:31
  • Note also that IsSelected = (PersonServices.IsActivated == null) ? false : true is equivalent to simply IsSelected = PersonServices.IsActivated != null. Commented Jan 22, 2019 at 15:32
  • If you broke the query up into multiple statements, rather than trying to force the whole thing into a single statement, the problem would either go away, or become apparent to you. Trying to force the whole query into a single statement is needlessly complicatin git. Commented Jan 22, 2019 at 15:41
  • @HenkHolterman It is a redline error. Commented Jan 22, 2019 at 15:49

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Beautiful. Thank you.
1

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

2 Comments

This is true, however if I close it, it will not recognize the sa alias. So I am not sure if there is a different way to structure this or not...
Also, I am needing to select all of it, not just something. My apologizes, I am a novice at linq. This does solve the issue however, but I really just want to shove it all into a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.