locked
Convert to List after FirstOrDefault RRS feed

  • Question

  • User-1401876114 posted

    Hello there I have a small requirement.

    My method is 

    public IEnumerable<County> GetCounty(int pK)
            {
    
                var County = _unitOfWork.GetCounty.Get().ToList();
                
               
     ...............
    ...............
               
            }

    When I do var x = FirstOrDefault(x=>x.ID==pk) or direct on the above line then I am not able to convert to back to List.

    I need to convert back to List for the codes below to use in same format, - Please help

    Thursday, September 27, 2018 1:53 PM

Answers

  • User475983607 posted

    By definition, FirstOrDefault() returns a type not a collection of types.  If you have a single type and need a collection of that type, then "new up" a collection and add the single type to the collection.

    var x = db.MyTypes.FirstOrDefault(x=>x.ID==pk); 
    List<MyType> myTypes = new List<MyType>();
    myTypes.Add(x);

    When I do var x = FirstOrDefault(x=>x.ID==pk) or direct on the above line then I am not able to convert to back to List.

    First, the syntax is invalid.  If you want a collection result then invoke an extension method that returns a collection rather than FirstOrDefault().

    List<SomeEntity> results = dbContext.SomeEntity.Where(x => x.Id == theId).ToList();

    I recommend learning the Generics, Collections, and Linq.  See the C# programming guide and Linq getting started tutorials.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-with-linq

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2018 2:54 PM

All replies

  • User475983607 posted

    By definition, FirstOrDefault() returns a type not a collection of types.  If you have a single type and need a collection of that type, then "new up" a collection and add the single type to the collection.

    var x = db.MyTypes.FirstOrDefault(x=>x.ID==pk); 
    List<MyType> myTypes = new List<MyType>();
    myTypes.Add(x);

    When I do var x = FirstOrDefault(x=>x.ID==pk) or direct on the above line then I am not able to convert to back to List.

    First, the syntax is invalid.  If you want a collection result then invoke an extension method that returns a collection rather than FirstOrDefault().

    List<SomeEntity> results = dbContext.SomeEntity.Where(x => x.Id == theId).ToList();

    I recommend learning the Generics, Collections, and Linq.  See the C# programming guide and Linq getting started tutorials.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/getting-started-with-linq

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2018 2:54 PM
  • User-1401876114 posted

    Thank You very much

    Friday, September 28, 2018 9:53 AM