Can you revert back to IList <T>?

In my service level for my MVC application, I am trying to convert linq to sql results into objects of my business model. I am currently trying to execute the following code:

    public IList<Project> GetAllProjects()
    {
        var results = from p in _context.Repository<DBMappings.project>()
                      select p;

        foreach (DBMappings.project prj in results.ToList<DBMappings.project>())
            yield return CreateProjectEntityFromDBProject(prj);

    }

Unfortunately, this does not work, and the only thing I can guess is that the output only works with IEnumerable. Is there any other solution besides creating a new list, adding elements to the foreach loop and returning the list? I need to use IList, because methods that use the returned list should be able to execute list methods such as .Sort ().

+3
source share
4 answers

.Sort(), . , - . , . -, .

+7

8.14 # ( ):

yield (§8.2), (§10.14.4) (§10.14.5) .

IList IEnumerable, IEnumerator.

+2

"" , , , :

return results.Select(prj=>CreateProjectEntityFromDBProject(prj)).ToList();

var results = from p in _context.Repository<DBMappings.project>()
                  select CreateProjectEntityFromDBProject(p);
return results.ToList();
+2

IList<T>describes functionality that is incompatible with IEnumerable<T>, at least until lazy loading.

The ability to go to a specific a la index IList<T>requires that the entire listing be “listed”, at least up to the specified index.

If you don’t need to evaluate everything IEnumerable<T>, you can build List<T>by taking a IEnumerable<T>constructor parameter, and then return List<T>asIList<T>

+1
source

Source: https://habr.com/ru/post/1735639/


All Articles