473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

LINQ: Related objects

Hi,

I have two classes (Review and Author) that are representative of tables
in my database. Both classes are marked up LINQ mappings. The database
(and classes) is structured in such a way that each Review has the ID of
the Author that wrote it, eg

[Table(Name = "Authors")]
public class Author
{
...
[Column(Name = "id", IsPrimaryKey = true, IsDbGenerated = true)]
public int ID
{
...
}
...
}

[Table(Name = "Reviews")]
public class Review
{
...
[Column(Name = "author")]
public int AuthorID
{
...
}
...
}

In the database, the "author" column in the "Reviews" table is a foreign
key mapped to the primary key (ID) of the "Authors" table.

What I am doing at the moment is getting an array of Review objects
using LINQ, and then looping through the array using a foreach
statement, then doing another query to get the Author object for each
Review, eg.

Review[] reviews = _db.GetReviews();
foreach (Review review in reviews)
{
Author author = _db.GetAuthorByID(review.AuthorID);
...
}

Now this to me seems awfully inefficient as it's going to doing loads of
queries to the database! Which brings me to my question - how can I do
this sort of thing in a more efficient way?

Can it be done keeping the Review and Author objects separate, or am I
going to have to create some sort of class that has all the information
about a review, ie. the review and author information? If so, how do I
go about writing a LINQ query to do this?

Thanks,

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jun 27 '08 #1
1 1253
Marc Gravell wrote:
You can express relations in LINQ via AssociationAttribute, and then
just navigate in the query; but setting these up correctly isn't
trivial; I'd suggest using the designer and seeing what it generates...
Thanks. I'll take a look into that.

--
Dylan Parry
http://electricfreedom.org | http://webpageworkshop.co.uk

The opinions stated above are not necessarily representative of
those of my cats. All opinions expressed are entirely your own.
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Johnson | last post by:
Just wondering if LINQ might be useful and appropriate in the following scenario: I'm writing a Windows Forms app that enables the user to search for a "client account". The client account is...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
8
by: Alcides | last post by:
Hello all, I learn about LINQ here in this forum. I been a VB.NET programmer for quite a while and we are using an internal solution for SQL access. I have some experience with C# and I started...
5
by: Andy B | last post by:
I was just wondering, when you create dataContext methods, should you put business logic there to try and minimize pushing data through 2-3 layers of code? or should the business logic still go in...
7
by: Andy B | last post by:
Just wondering why linq is more useful than datasets? The stuff I do doesn't seem to be too complicated to use linq with it. If I did use linq with it now, I would be doing almost the exact same...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
5
by: ck1 | last post by:
Hi at all - I want to know if there are diffirence with Liqn to SQL and Linq To entity performance. I have read many article on the web that say that Linq To Entity is more fast than Linq To...
9
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.