Why Lambda expression returns System.Linq.Enumerable+WhereSelectEnumerableIterator`2 as a result

I'm getting following string as result while returning a list of string using lambda expression:

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[HOrg.ServiceCatalog.Contracts.Models.IOfferProperty,System.String]

My code is:

    IList<string> offerIds = new List<string>();
    foreach (var offer in offerProperties)
    {
       offerIds.Add(offer
         .Where(x => x.PropertyDefinitionId == propertyDefinitionId)
         .Select(x => x.OfferId)
         .ToString());
    }

Within foreach loop, offer variable contains expected values. But when I make condition using lambda expression, it returns System.Linq.Enumerable+WhereSelectEnumerableIterator`2 as a result.

When I search for this, I got a few suggestions like,

  1. Copying results of lambda expressions in to a seperate list
  2. Use ToList() for lambda expression then assign it to a result variable

and more suggestion. But no answer is helpful for me.

Is anybody know what's wrong in this code?

Instead of converting sequence to String:

 // How can .Net convert sequence into string? The only way is to return type name
 // which is unreadable System.Linq.Enumerable+WhereSelectEn... 
 offer
   .Where(x => x.PropertyDefinitionId == propertyDefinitionId)
   .Select(x => x.OfferId)
   .ToString()

Join the items into a string

  // Join items into string with "; " delimiter, e.g. "1; 2; 3; 4"
  offerIds.Add(string.Join("; ", offer
    .Where(x => x.PropertyDefinitionId == propertyDefinitionId)
    .Select(x => x.OfferId)));

Why Lambda expression returns System.Linq.Enumerable+ , But when I make condition using lambda expression, it returns System.Linq. Enumerable+WhereSelectEnumerableIterator`2 as a result. A lambda expression cannot directly capture an in, ref, or out parameter from the enclosing method. A return statement in a lambda expression doesn't cause the enclosing method to return. A lambda expression cannot contain a goto, break, or continue statement if the target of that jump statement is outside the lambda expression block. It's also

If you expect a single result for every offer, try:

IList<string> offerIds = new List<string>();
foreach (var offer in offerProperties)
{
   offerIds.Add(offer.Where(x => x.PropertyDefinitionId == propertyDefinitionId).Select(x => x.OfferId).FirstOrDefault()?.ToString());
}

Lambda expressions, A lambda expression that has one parameter and returns a value can be You can also use lambda expressions when you write LINQ in C#, as the following When you call the Queryable.Select method in the System.Linq. Lambda Expression and Objects. In Java, any lambda expression is an object as is an instance of a functional interface. We can assign a lambda expression to any variable and pass it like any other object. See the example below on how a lambda expression is assigned to a variable, and how it is invoked. Where you can use Lambda expressions

It seems to me that you want a collection of offerIds as a string, where multiple are attached to the offerproperties.

If so, then you are looking for the addrange function. Also move your ToString() call inside the select statement, not after it.

IList<string> offerIds = new List<string>();
foreach (var offer in offerProperties)
{
    offerIds.AddRange(offer.Where(x => x.PropertyDefinitionId == propertyDefinitionId).Select(x => x.OfferId.ToString()));
}

Now for each offer, a selection of OfferId-strings is added to your offerIds IList

Expression.Lambda Method (System.Linq.Expressions), IEnumerable<System.Linq.Expressions.ParameterExpression> parameters); The LambdaExpression type is used to represent the returned object because� How lambda expressions are converted to objects of functional interfaces is a more complicated story. 3. Syntax of Lambda Expression. The syntax of a lambda expression consists of the following: A comma-separated list of formal parameters enclosed in parentheses. In this case, it is (Dog m, Dog n) The arrow token -> A body, which consists of a

Lambda Expressions - Using C# LINQ, Here is an example of what a lambda expression looks like: Func<int, int> multiplyByFive = num => num * 5; // Returns 35 int� The Boolean value that the Any<TSource>(IEnumerable<TSource>) method returns is typically used in the predicate of a where clause (Where clause in Visual Basic) or a direct call to the Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) method. The following example demonstrates this use of the Any method.

Language Integrated Query (LINQ) and Lambda Expressions, foreach (var msg in query) The first usage infers that an IEnumerable<Message > will be returned and assigned to the query variable. The second usage infers� Java Lambda Expressions. Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.

Anatomy of the Lambda Expression, C# 3.0(.NET 3.5) introduced the lambda expression along with LINQ. Dim isStudentTeenAger = Function(s As Student) As Boolean Return s. class includes Where extension method for IEnumerable<T> that accepts Func< TSource,bool> . Lambda Expressions and Compilation. The expression tree represents an idea. It needs to be turned into something material. In case the expression can be simplified, I reduce it. Next, I create a lambda expression. This defines the shape of the parsed expression, which will be a predicate (Func<T,bool>). Finally, I return the compiled delegate.

Comments
  • Join all the items of the sequence: string.Join("; ", offer.Where. ... .Select(x => x.OfferId))
  • You should realise that that's the default result of calling ToString on a type that hasn't overridden the one from Object. Which means it's almost certainly not meant to have ToString() called on it outside of debug scenarios..
  • Would be better to suggest: offer.FirstOrDefault(x => x.PropertyDefinitionId == propertyDefinitionId)?.OfferId.ToString()
  • I have tried what you suggest. Even all the valid records also returning null.
  • @novicedeveloper - we don't have access to your code or your data. Nobody can provide you a tested working solution unless you give us more information. A good start would be a minimal reproducible example and a clear description of what the code is meant to be achieving