How is LINQ used in ASP.NET web programming?
I am worki开发者_StackOverflowng in my own website project. I am using a DataAdapter to fetch data from a database. How can I use LINQ instead?
You can leverage LINQ in one of the following ways in your case:
- Use LINQ to SQL if your database is SQL Server
- Use LINQ to DataSets if you work with DataSet/DataTable
- Use LINQ to entities for a database your accessing via the Entity Framework
- Use LINQ to Objects at will
To access data using LINQ to SQL, you need to create a DataContext. Visual Studio has a designer for Data context classes in which you can simply drag and drop tables from the server explorer, or use SQLMetal to reverse engineer your database. But this only works for SQL Server databases.
For a generic database, you will need to use Entity Framework, for which you can generate the Entity class design using EDM Generator.
If you want to continue using your current approach, you can just call AsEnumerable method on a DataTable in the DataSet, and it will give you an IEnumerable<DataRow>, on which you can use LINQ operators to your heart's content.
精彩评论