Is LINQ the best solution in this scenario? LINQ to Dataset
Is LINQ the best solution for dat开发者_Python百科a to object automatic mapping using .NET? Can anyone suggest me some other solution the create automatically the database table and stored procedure mapping ?
Is it possible convert LINQ( or other solution) result into
Dataset
orDatatable
automatically ?
LINQ is not an ORM. It's just a language extension that provides SQL-query-like syntax and queryable types that "integrate" query expressions to your programs. It does not map objects to the database whatsoever.
That said:
LINQ to SQL is an ORM that uses LINQ. Again, as you see, LINQ itself isn't an ORM, but can be used in an ORM framework. But as mentioned by AHM, also take a look at Entity Framework.
A well-known open source .NET ORM solution is NHibernate which is really a .NET port of Hibernate for Java.
MSDN has articles on LINQ to DataSet, but this isn't related to object-relational mapping.
Populating Datasets or Datatables with LINQ
LINQ to Dataset - Getting Started
LINQ to Dataset - Programming Guide
LINQ Query to Datatable Example
var query1 = from i in items
where i.Price > 9.99
orderby i.Price
select i;
// load into new DataTable
DataTable table1 = query1.CopyToDataTable();
LINQ to SQL Alternatives
- Entity Framework
- NHibernate for .NET
For a new application you should probably use either Entity Framework or NHibernate. Both are mature ORM solutions and both support LINQ for language integrated queries.
LINQ-to-SQL is an option, but it's pretty much deprecated in favor of Entity Framework. You shouldn't use it for a new application.
DataSets are pretty much the opposite of ORM. They're creating an in-memory representation of your relational data in an object whose sole purpose is to represent that data. They work for very quick results or when your data is truly dynamic, but generally are far inferior to any true ORM solution.
Arthur, your question is not really answerable in any meaningfull way as it is written.
Linq stands for Language Integrated Query, and by itself is not an ORM (Object relational Mapping) framework. There are however ORM's that do make heavy use of Linq, Microsoft have 2 flavours, Linq to SQL and Linq to Entities Framework, there are also 3rd party ORM's that also make use of Linq..NHibernate for example.
Then your follow up question of convert Linq to Dataset / Datatable, while not actually making any sense also implies that you would rather be working with or binding to database middleware components, this would make using an ORM pretty redundant in most cases.
What an ORM does is allows you to map your domain model to a data model, so that in your application you never need to think in terms of your data model and structures you just think in terms of your domain objects....your code would look like this...
IEnumerable<Customer> Custs = someRepository.Customers();
foreach(Customer c in Custs)
{
// Do something with the custs.
}
Instead of...
var cmd = connection.CreateCommand();
cmd.CommandText = "Select * from customers";
using(var reader = cmd.ExecuteReader())
{
// etc etc
}
Maybe if you could post a little more of what you are trying to achieve, we could give you some better advice.
1) LINQ is a query language it is not an ORM. Just see the wiki page for available ORMs. It is too hard to get the one that suits you best without knowing where you will use it
2) Suggested by hamlin11 + Castle Active Record looks like a DataTable. But I wouldn't mix DataSet and DataTable with ORM as this technologies use different memory and data update strategies
While LinqToSQL is not technically and ORM, it's a perfectly good substitute if you want basic functionality like direct table to object mapping.
The other MS tool is Entity Framework, which supports, Model first, Database first and Code first models.
Entity Framework is more advanced then LingToSQL as it allows you to split and combine tables to different entites and a lot more advanced options. There are a lot of good video's at http://www.asp.net or at Pluralsight
I'm not sure if Linq can easily be converted to datasets and datatables but there isn't really any need to, it's just changing from one data object type to another.
精彩评论