Return current item, in lamba method of Select.

first100 81 Reputation points
2021-02-26T10:42:32.297+00:00

Hello,

i have an external list of items homogeneous with my entities,
i have to update the entities setting a flag to true,

After many attempts this is my code :

 var insideItems= outsideItems.ToList().Select(x => { x.IsNew = true; return x; });

the code working fine, the outsideItems is a IQueryAble list of my entities, but my question is, why i need to have a "return x" in code and i cannot do:

 var insideItems= outsideItems.ToList().Select(x => { x.IsNew = true; });

just setting the flag, due the error : "The type arguments for method 'Enumerable.Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, int, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly."

Sorry for newby question but i would to understand better.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,067 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,481 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,286 Reputation points
    2021-02-26T18:13:16.503+00:00

    Select expects a return of type T (your concrete class). In the following. We don't need to be concerned with IQueryable aspect, just a the .List -> Select.

    Using

    namespace Example1.Classes  
    {  
        public class Person  
        {  
            public string FirstName { get; set; }  
            public string LastName { get; set; }  
            public int Id { get; set; }  
            public bool Active { get; set; }  
            public bool IsNew { get; set; }  
        }  
    }  
    

    We can change a property value, in this case all person objects in the list.

    Change IsNew

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using Example1.Classes;  
      
    namespace Example1  
    {  
        public class Operations  
        {  
            public Operations()  
            {  
                List<Person> list = GetPeopleList();  
                list.ForEach(person => person.IsNew = true);  
            }  
            public List<Person> GetPeopleList()  
            {  
      
                return new List<Person>  
                {  
                    new Person()  
                    {  
                        Id = 1,  
                        FirstName = "Pete",  
                        LastName = "Smith",  
                        Active = true,   
                        IsNew = false  
                    },  
                    new Person()  
                    {  
                        Id = 2,  
                        FirstName = "Karen",  
                        LastName = "Payne",  
                        Active = true,  
                        IsNew = false  
                    },  
                    new Person()  
                    {  
                        Id = 3,  
                        FirstName = "Jane",  
                        LastName = "Williams",  
                        Active = true,  
                        IsNew = false  
                    }  
                };  
            }  
      
      
        }  
    }  
      
    

    72500-11111111111.png

    0 comments No comments