How do I make my Linq for a Sql class IEnumerable

How to create a Linq class for Sql IEnumerable or IEnumerable object in C # 3.0

0


a source to share


3 answers


If you are talking about generated LinqToSql classes you should do it in a partial class



public partial class YourLinqToSqlClass : IEnumerable
{
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        //Implement...
    }

}

      

0


a source


To make an object enumerable in C # you must implement the IEnumerable interface

public class Widget{}
public class WidgetCollection : IEnumerable<Widget>
{
    public IEnumerator<Widget> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

}

      



As for the second part of your question, I'm not sure what you are asking or trying to do.

+2


a source


I'm not sure what you mean by "make my Linq for the SQL class IEnumerable". However, in your controller (assuming ASP.NET MVC) ...

WidgetDataContext dataContext = new WidgetDataContext();

var data = dataContext.Widgets.OrderBy(x => x.name);

return View(data);

      

In this case, the view will be able to just draw the object data

(named Model

in the view) as IEnumerable<Widget>

it can pass its path through it.

Does this mean (with Josh's answer above) the question?

0


a source







All Articles