471,310 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 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 1175
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bob Johnson | last post: by
22 posts views Thread by paululvinius | last post: by
8 posts views Thread by Alcides | last post: by
7 posts views Thread by Andy B | last post: by
9 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
5 posts views Thread by ck1 | last post: by
9 posts views Thread by Cirene | last post: by
reply views Thread by rosydwin | last post: by

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.