score:2
if you are working with sql server, ado.net + linq-to-sql is definitely the way to go.
reasons for linq:
- it fits naturally with your .net code, ide, and debugger
- you don't have to maintain your code in one place and your queries in another
- you aren't hard-coding sql queries and you don't have to go to the trouble of creating paramterized sql (which is really a pain in my opinion)
- it is super fast to write and surprisingly flexible
- your code will be cleaner and smaller because you don't have to have ado.net and database plumbing everywhere - linq takes care of all the hard stuff
- you get a nice entity designer that allows you to drag-n-drop from a sql connection
reasons not to use linq:
- if you have to squeeze every last ounce of performance out of a query, linq may not be the most natural choice because sql can sometimes be more flexible. but you should really consider your query carefully to see if there is an easier way to do it.
- some places don't like the security implications because the tables have to be public to your connected user. some companies only allow access to tables via sprocs.
for your example, you'll want to create classes (poco's) that represent your database entities. then you'll use linq to query your tables and load data into the new entity objects. i prefer to have a constructor on my domain/entity objects that transform the persistent database objects generated by linq into domain-layer objects to be used by the client.
public class person
{
public int id { get; set; }
public string surname { get; set; }
public string name { get; set; }
public short age { get; set; }
public person()
{
}
public person( persistence.person src )
{
this.id = src.id;
this.surname = src.surname;
this.name = src.name;
this.age = src.age;
}
}
...
public list<domain.person> loadpeople()
{
using( var context = this.createcontext() )
{
var personquery = from p in context.persons
select new domain.person( p );
return personquery.tolist();
}
}
public person loadperson( int personid )
{
using( var context = this.createcontext() )
{
var personquery = from p in context.persons
where p.id == personid
select new domain.person( p );
return personquery.singleordefault();
}
}
score:1
assuming you have the following tables:
tblusers
userid lname fname genderid
1 joe chan 1
2 koh wang 2
3 john chen 1
tblgenders
genderid gender
1 male
2 female
you can use this code to get a dataset that would contain the combined information from the two tables above.
sqlconnection con = new sqlconnection(connection string for you database);
con.open();
sqlcommand comm = con.createcommand();
comm.commandtext = "select u.userid, u.fname, u.lname, u.genderid, g.gender
from tblusers u, tblgenders g
where u.genderid = g.genderid";
sqldataadapter da = new sqldataadapter(comm);
dataset ds = new dataset();
da.fill(ds);
you can access the rows under the table by using this code da.table[0].rows;
if you are already knowledgeable with stored procedures you can also use this code:
internal static list<roomtype> fetchroomtypelist()
{
list<roomtype> roomtypes = new list<roomtype>();
sqlcommand commroomtypeselector = connectionmanager.mainconnection.createcommand();
commroomtypeselector.commandtype = commandtype.storedprocedure;
commroomtypeselector.commandtext = "rooms.asp_rms_roomtypelist_select";
sqldataadapter da = new sqldataadapter(commroomtypeselector);
dataset ds = new dataset();
da.fill(ds);
roomtypes = (from rt in ds.tables[0].asenumerable()
select new roomtype
{
roomtypeid = rt.field<int>("roomtypeid"),
roomtypename = rt.field<string>("roomtype"),
lasteditdate = rt.field<datetime>("lasteditdate"),
lastedituser = rt.field<string>("lastedituser"),
isactive = (rt.field<string>("isactive") == "active")
}
).tolist();
return roomtypes;
}
for more information visit:
Source: stackoverflow.com
Related Query
- Rookie thing: How to collect data from multiple tables most deftly?
- LINQ - How to get data from multiple tables
- How to pass a LINQ statement getting data from multiple tables to a model to populate a View?
- Linq and RESTful services: how to best merge data from multiple tables in a resultset
- How to retrieve data from multiple tables using Entity Framework
- How to retrieve data from multiple tables that may or may not have values in common
- How to select data from multiple tables with LINQ (entities)
- EF Lambda Expression How to Access Data from Multiple Tables
- How to MVC C# Viewmodel to join data from multiple tables
- How to access data from multiple tables
- How to search data from multiple tables using in EF
- How to get all data from multiple tables linked by foreign keys with Entity Framework?
- How to get rows of Data from Multiple Tables using LinQ To Entities
- How to inner join tables from different Data Context?
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- How to reuse a linq expression for 'Where' when using multiple source tables
- How does linq actually execute the code to retrieve data from the data source?
- Get data from multiple tables while editing in MVC
- Select data from multiple unrelated tables with LINQ to Entity Framework
- How to bind data from mutiple tables to datagridview using Entity Framework and use CRUD operations?
- How do I return data from joined tables through subsonic's objects?
- Pull data from multiple tables in one SQL query using LINQ and Entity Framework (Core)
- C# LINQ How to get a data source from a db?
- How can I join datasets from multiple tables with only certain columns in EF Core?
- Retrieving Data From Multiple Tables in a LINQ to SQL query
- How do I return single data from primary table and multiple entries in second table nested after join
- Returning data from multiple sql tables entity framework web api
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- Join data from multiple tables into SelectList, and precede each dataset with a caption in MVC
- How to get entities from multiple tables with one LINQ call?
More Query from same tag
- How to Convert Row to Column in Linq and SQL
- Enumerable LINQ extensions are hidden on strings... why and how?
- How to convert whole Bson document to a string in dotnet
- LINQ: transparent record inserting and updating
- What's the syntax for returning a collection of collections?
- Where can I get information on the ! operator used by VB.Net in Linq to DataSet?
- Converting SQL to LINQ to XML
- Declaring DataRow within a foreach or for Loop
- LINQ/lambda: How can I query a DB table based on information from another table? (many to many relationship)
- How to register CLR User Defined Function for use in linq query?
- Populating IEnumerable<MyType> using Linq gives error "The specified type member is not supported in LINQ to Entities."
- Is it possible to cancel select and 'Continue' within .Select statement upon a condition?
- Complex Linq query
- Does LINQ-to-SQL Support Composable Queries?
- How do I query identity data efficiently in ASP.Net Core?
- LINQ Group by - linq to entities does not recognize the method 'char get_chars(int32) method
- Is my C#.NET LINQ expression optimized for deferred execution (Entity Framework Core)
- Comparing Dictionaries using LINQ in C#
- Find the number that don't exist in another array
- Accessing attributes on fields using extension method
- How to use linq to print variable from object in comma separated?
- LINQ Select within a Select
- Entity framework - how to filter eager loaded navigational/relational properties?
- how to order a group result with Linq?
- Passing IEnumerable data from LINQ as parameter to a method
- Join unique strings from an array of objects
- Show joined value on bound field without custom type?
- Unable to create a constant value. Only primitive types
- Can I use EF and Linq, if I am getting connectionstring at runtime?
- LINQ Where statement in collection