How to retrieve data with LINQ to SQL into a weakly-typed object that can be moved around?

You should qualify what you mean by "move around". Much of what you might have done in Classic ASP is now considered to be a bad practice, be careful not to repeat well known mistakes which have had solutions for some years now Also, "var" is strongly-typed, just anonymous: "no name", not "no type Please say what you mean about "proper OO" not permitting data to be both fetched and used in the same place Also, remember that examples are meant to show off individual features. No single example is going to show you everything.In particular, the answer to "how do I return an anonymous type" is "you don't return anonymous types".

For instance: using (ormDataContext context = new ormDataContext(connStr)) { var electionInfo = from t1 in context. Elections join t2 in context. Election_status on t1.

StatusID equals t2. StatusID select new ElectionWithStatus { Election=t1, Status=t2 }; } Where: public class ElectionWithStatus { public Election Election {get;set;} public ElectionStatus Status {get;set;} } So, bottom line, if you have to move it around, then move a strongly-typed object, not a weakly-typed object.

You should qualify what you mean by "move around". Much of what you might have done in Classic ASP is now considered to be a bad practice, be careful not to repeat well known mistakes which have had solutions for some years now. Also, "var" is strongly-typed, just anonymous: "no name", not "no type".

Please say what you mean about "proper OO" not permitting data to be both fetched and used in the same place. Also, remember that examples are meant to show off individual features. No single example is going to show you everything.In particular, the answer to "how do I return an anonymous type" is "you don't return anonymous types".

For instance: using (ormDataContext context = new ormDataContext(connStr)) { var electionInfo = from t1 in context. Elections join t2 in context. Election_status on t1.

StatusID equals t2. StatusID select new ElectionWithStatus { Election=t1, Status=t2 }; } Where: public class ElectionWithStatus { public Election Election {get;set;} public ElectionStatus Status {get;set;} } So, bottom line, if you have to move it around, then move a strongly-typed object, not a weakly-typed object.

By move around I meant return from a method or assign to an object property, i.e. Move out of the local context. By "proper OO" I mean, for example, retrieving the data in one object, and actually utilizing in another one, i.e.

Separation of concerns, data access layer, etc. I'm not sure I understand if the electionInfo anonymous type can now be returned from the function, but will try it out, thx. – alchemical Jul 2 '09 at 17:32 "electionInfo" is an instance of an anonymous type implementing IQueryable. The fact that it implements an interface allows any code that gets ahold of it to work with it in a strongly-typed manner.

With just "select new {...}", you'd be returning an instance of IQueryable where "T" was unknown - the caller would have no idea what was inside it. – John Saunders Jul 2 '09 at 18:07.

This is how I would do it. I would create a foreign key relationship (and index if appropriate) between the two tables. I would create a new Linq to Sql Classes object in my project, which creates a DBML file.

I'd open the DBML file to get the designer surface for my new data context and drag both tables from the Server Explorer -- after creating the connection to the DB, of course. This would create the entities matching the two tables in my data context and the association between them. This would give me an EntityRef in the Election entity corresponding to the Election_Status -- or an EntitySet depending whether it is a 1-1 or 1-Many relation.

I'd use the association instead of doing a join to get the data for each election. Using (var context = new ormDataContext(connStr)) { foreach (var election in context. Elections) { Console.

WriteLine( "{0}: {1}", election. Name, election. Election_Status.

Status ); } } If I wanted to filter by a particular status, I could do that as well. Using (var context = new ormDataContext(connStr)) { var query = context. Elections .

Where( e => e. Election_Status. Status == someStatus ); foreach (var election in query) { Console.

WriteLine( "{0}: {1}", election. Name, election. Election_Status.

Status ); } }.

If you are dead set on LINQ, you'll have to extract what you need from the variant ("electionInfo") return value and pass that around, instead of passing around the electionInfo variant itself. In some cases, this might mean making a custom object. (though, I don't know why this is such an issue, as you probably would have made a custom object anyway if you weren't using LINQ or if you were using classic ASP's record set).

You could also simply ignore LINQ and use the 'SqlCommand..ExecuteReader' method which returns a SqlDataReader. This is how most Sql queries were read prior to LINQ. My advice?

Without going into too much detail, I think the advantages of LINQ outweigh the extra boilerplate code that is needed to use SqlDataReader.

Data with linq is retrieved (without quotes):. Retrieve data with Linq.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions