logo
down
shadow

Return type as IEnumerable instead of just List?


Return type as IEnumerable instead of just List?

By : user2173141
Date : October 21 2020, 08:10 PM
will be helpful for those in need I am doing a .NET MVC tutorial. With that being said, I've came across code like this: ,
Return type as IEnumerable instead of just List?
code :
//controller

var a = new Dictionary<string, string>();
return View(a);

var b = new List<string>();
return View(b);

var c = new LinkedList<string>();
return View(c);

// All work with:

@model IEnumerable<string>
// Controller Returns:
var people = .... as IEnumerable<Person>;
return View(people);

@model IEnumerable<Person>
public class MyViewModel
{
  public string Title { get; set;}
  public IEnumerable<Person> People { get; set;}
}

return View(myViewModel);

@model MyViewModel
    public ActionResult Index()
    {
        var movies = GetMovies();

        return View(movies);
    }

    private IEnumerable<Movie> GetMovies()
    {
        return new List<Movie>
        {
            new Movie {Id = 1, Name = "Shrek"},
            new Movie {Id = 2, Name = "LotR"}
        };
    }
public class MoviesController : Controller
{
    private readonly IMovieDb _movieDb;
    // Dependency Injecting access to movies
    public MoviesController(IMovieDb movieDb)
    {
        _movieDb = movieDb;
    }

    public ActionResult Index()
    {
        var movies = _movieDb .GetMovies();

        return View(movies);
    }
    // ....


public interface IMovieDb
{
    IEnumerable<Movie> GetMovies();
}


Share : facebook icon twitter icon
List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?

List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?


By : Mihai Coman
Date : March 29 2020, 07:55 AM
I hope this helps you . Collections are not generally very useful for DAL returns, because a collection does not implicitly guarantee order. It's just a bucket of items. An IList, on the other hand, does implicitly guarantee order. So we're down to IEnumerable or IList. The next question would be: is the List object "live"? i.e., is it connected to the data backing so that when you add an item to the IList, it will be reflected in the DB? For LINQ-to-SQL, this is not the case. Rather, you're supposed to attach entities to the tables. So unless you supply this additional wiring, a List is superfluous. Stick with IEnumerable.
Why can I not return List<Entry> when the return type is IEnumerable<IEntry>?

Why can I not return List<Entry> when the return type is IEnumerable<IEntry>?


By : Emanuel Kardinal
Date : March 29 2020, 07:55 AM
this one helps. I believe that this is only available in .net 4
in earlier versions you can:
code :
using System.Linq;

IEnumerable<IEntry> func()
{
    return new List<Entry>().Cast<IEntry>();
}
How to return json list result in IENumerable type viewbag MVC?

How to return json list result in IENumerable type viewbag MVC?


By : Satiz Kumar
Date : March 29 2020, 07:55 AM
wish helps you I am trying to get list result in IENumerable type ViewBag that contains already a list before. , Do you mean something like this inside your success handler:
code :
if(data){
    data.forEach(function(item){
        $('#yourtagcontainer').append('<p class="tag-division"><a id="'+item+'" class="tag-in-dropdown" href="#"># '+item+'</a></p>');
    });
}
Using return type List<T> vs IEnumerable<T>

Using return type List<T> vs IEnumerable<T>


By : daodaoliang
Date : March 29 2020, 07:55 AM
it helps some times If Resharper could know that some foo callers will want to access random elements in the List, it would not suggest you return IEnumerable. As soon as only you have all picture, you should follow or not Resharper's suggestions, which are often very good though.
Why does returning a List from an out argument of type IEnumerable not return the items from the list?

Why does returning a List from an out argument of type IEnumerable not return the items from the list?


By : Bearviking
Date : March 29 2020, 07:55 AM
With these it helps The declared type of records is IEnumerable. So even after you've assigned a List to that variable, you can only (directly) call methods that are supported by the IEnumerable interface.
There's no reason why you have to use that particular variable whilst you're creating and populating your list:
Related Posts Related Posts :
  • How do request a correct access token in ASP.NET Core for Azure AD to access Microsoft Graph
  • How to run a .NET Core console application on Linux
  • UI not updating for bound element
  • C# Odata v4 open type raw values
  • DocumentFormat.OpenXml Modify Creator Propery of Document
  • How to SetBasePath in ConfigurationBuilder in Core 2.0
  • ASP MVC5 public readonly variable changes value when it should not
  • How to conditionally select item from hashset with queryable linq?
  • Progress Bar C# not showing the real progress
  • Correct logic to reconnect in SignalR with HubConnection
  • ComboBox with two or more sections
  • .NET listbox to SQLite DB
  • C# Ninject binding from Dictionary<Type, Type>
  • asp.net mvc -view displaying real-time progress status
  • Getting reference to dependent in service with generic type
  • Implementing EqualityCompare vs overriding GetHashCode and Equals
  • C# Linq Find all indexes of item in List<int> within another List<int>
  • XML image how to display at data grid columns in c#.net
  • How to override Equals and GetHash of HashSet
  • Creating Windows authentication and SQL Server authentication in login form
  • How to interop with Azure Service Bus topics created by MassTransit using Rebus?
  • MVC 5 Google Calendar Integration
  • C# - Use parallel arrays to calculate cost of a phone call in GUI
  • Use a int in entire code
  • How to display newly added element at the top of StackPanel in WPF?
  • T4MVC is generating T4MVC.cs and T4MVC1.cs
  • Remove blur effect on certain controls inside UserControl
  • Sql Transcation Query to Linq Query in Wcf Rest Service
  • Setting AWS S3 credentials in C# app
  • does not contain in an ObservableCollection
  • App doesn't run it crashed directly after I started it
  • Is it possible in Entity Framework MVC application to run .sql scripts on a different database
  • Type Inference with Deconstruct tuple assignment extension methods
  • Setting up mock objects for EF dbcontext to test repository methods
  • Using C# in XSLT with MSXSL 6.0
  • Absolute positioning of UIElement in FlowDocument in WPF
  • Display debuggers not showing up in Visual Studio 2017
  • Discord.NET Users playing the same game
  • Azure function output API call to 3rd party service
  • Unable to determine the relationship represented by navigation property ASP.NET core 2.0 Entity Framework
  • Regex to find group of Regex inside a word
  • When calling a method, when to reference the class, and when to reference the object?
  • AmazonCloudWatchLogsClient async methods failing with no error
  • Create FileStream in memory instead of saving a physical file on disk
  • ViewBag data coming from ActionFilter is persisting in session even after database
  • ERROR: the name 'lvSubCategories' does not exist in the current context
  • Why datetime can't recognise the string?
  • Getting 500 error while requesting to webservice using Ajax AutoCompleteExtender
  • The property 'x' is not a navigation property of entity type 'y'
  • ASP.NET MVC - How to generate empty input boxes in loop when model is empty?
  • Need help writing regular expression
  • Add/remove buttons to a panel with an arraylist c#
  • Linq .Where(type = typeof(xxx)) comparison is always false
  • Using EF6 Code First without migrations to an existing db how do I specify column names of the many to many join table?
  • C# - Using Thread.Sleep precisely in a console application
  • Add item to a List<Tuple> inside another list
  • Double[,], inversion C#
  • Counting a string to ensure the input is within a min/max boundary
  • Assign invalidValue to Enum variable (why is this not throwing an Exception?)
  • Namespace or type specified in the project-level Imports 'System.Threading.Tasks' doesn't contain any public member or c
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org