473,796 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LINQ. Who knows how to do this?

Hello,

I created 3 few tables that have Many to Many relationships:

Posts (PostId PK)
Files (FileId PK)

PostsTags (PostId PK, TagId PK)
FilesTags (FileId PK, TagId PK)

Tags (TagId PK, TagName)

First of all, by dragging the tables to a dbml file the classes are
not ready for such Many to Many relationship:

Posts PostsTags Tags
and
Files FilesTags Tags

Does anyone knows how to do this?

And the behavior which seems logic to me (but maybe I am wrong) should
be:
(The example for Posts. For files is the same)

- When a tag is added to a Post, a record is added to TagsInPosts.
If the Tag (given its TagName) does not exist in Tags then it is
created in Tags table.

- When a tag is removed from a post then it is deleted from PostsTags.
If that tag is no longer associated with a Post or a File through
PostsTags and FilesTags then it is deleted from Tag.

Can I implement this logic in my LINQ classes or should I need to
create triggers, besides the constrains I enumerated, in my database?

I only find simple one-to-one examples and the only many-to-many
example is back to May 2006.

Thanks,
Miguel

Oct 17 '07 #1
2 1362
Miguel,

In your database do you have relationships setup. When you create your LINQ
to SQL classes it uses the relationship data to determine the shape of the
objects. I created the 5 tables that you mentioned;

Post
File
Tag
PostTag
FileTag

I then setup the relationships inside if Enterprise manager. Created a LINQ
to SQL class including all of the tables. and then wrote the following
query;

LinqTestDbDataC ontext db = new LinqTestDbDataC ontext();

db.Log = Console.Out;

var values = from file in db.Files

select new { file.FileTag.Ta g };

foreach (var value in values)

{

Console.WriteLi ne(value.Tag.Ta gID.ToString()) ;

}

with the logging on I inspected the SQL that was created for execution

SELECT [t3].[test], [t3].[TagID], [t3].[TagDescription]

FROM [dbo].[File] AS [t0]

LEFT OUTER JOIN [dbo].[FileTag] AS [t1] ON [t1].[FileTagID] = [t0].[FileID]

LEFT OUTER JOIN (

SELECT 1 AS [test], [t2].[TagID], [t2].[TagDescription]

FROM [dbo].[Tag] AS [t2]

) AS [t3] ON [t3].[TagID] = [t1].[TagID]

As you can see it does work with Many-to-Many relationships. Just make sure
that you have relationships defined in the database.
Mark
"shapper" <md*****@gmail. comwrote in message
news:11******** **************@ e34g2000pro.goo glegroups.com.. .
Hello,

I created 3 few tables that have Many to Many relationships:

Posts (PostId PK)
Files (FileId PK)

PostsTags (PostId PK, TagId PK)
FilesTags (FileId PK, TagId PK)

Tags (TagId PK, TagName)

First of all, by dragging the tables to a dbml file the classes are
not ready for such Many to Many relationship:

Posts PostsTags Tags
and
Files FilesTags Tags

Does anyone knows how to do this?

And the behavior which seems logic to me (but maybe I am wrong) should
be:
(The example for Posts. For files is the same)

- When a tag is added to a Post, a record is added to TagsInPosts.
If the Tag (given its TagName) does not exist in Tags then it is
created in Tags table.

- When a tag is removed from a post then it is deleted from PostsTags.
If that tag is no longer associated with a Post or a File through
PostsTags and FilesTags then it is deleted from Tag.

Can I implement this logic in my LINQ classes or should I need to
create triggers, besides the constrains I enumerated, in my database?

I only find simple one-to-one examples and the only many-to-many
example is back to May 2006.

Thanks,
Miguel

Oct 17 '07 #2
On Oct 17, 8:06 pm, "Mark Dykun" <mdykun1...@rog ers.comwrote:
Miguel,

In your database do you have relationships setup. When you create your LINQ
to SQL classes it uses the relationship data to determine the shape of the
objects. I created the 5 tables that you mentioned;

Post
File
Tag
PostTag
FileTag

I then setup the relationships inside if Enterprise manager. Created a LINQ
to SQL class including all of the tables. and then wrote the following
query;

LinqTestDbDataC ontext db = new LinqTestDbDataC ontext();

db.Log = Console.Out;

var values = from file in db.Files

select new { file.FileTag.Ta g };

foreach (var value in values)

{

Console.WriteLi ne(value.Tag.Ta gID.ToString()) ;

}

with the logging on I inspected the SQL that was created for execution

SELECT [t3].[test], [t3].[TagID], [t3].[TagDescription]

FROM [dbo].[File] AS [t0]

LEFT OUTER JOIN [dbo].[FileTag] AS [t1] ON [t1].[FileTagID] = [t0].[FileID]

LEFT OUTER JOIN (

SELECT 1 AS [test], [t2].[TagID], [t2].[TagDescription]

FROM [dbo].[Tag] AS [t2]

) AS [t3] ON [t3].[TagID] = [t1].[TagID]

As you can see it does work with Many-to-Many relationships. Just make sure
that you have relationships defined in the database.

Mark

"shapper" <mdmo...@gmail. comwrote in message

news:11******** **************@ e34g2000pro.goo glegroups.com.. .
Hello,
I created 3 few tables that have Many to Many relationships:
Posts (PostId PK)
Files (FileId PK)
PostsTags (PostId PK, TagId PK)
FilesTags (FileId PK, TagId PK)
Tags (TagId PK, TagName)
First of all, by dragging the tables to a dbml file the classes are
not ready for such Many to Many relationship:
Posts PostsTags Tags
and
Files FilesTags Tags
Does anyone knows how to do this?
And the behavior which seems logic to me (but maybe I am wrong) should
be:
(The example for Posts. For files is the same)
- When a tag is added to a Post, a record is added to TagsInPosts.
If the Tag (given its TagName) does not exist in Tags then it is
created in Tags table.
- When a tag is removed from a post then it is deleted from PostsTags.
If that tag is no longer associated with a Post or a File through
PostsTags and FilesTags then it is deleted from Tag.
Can I implement this logic in my LINQ classes or should I need to
create triggers, besides the constrains I enumerated, in my database?
I only find simple one-to-one examples and the only many-to-many
example is back to May 2006.
Thanks,
Miguel
Mark,

I am checking everything in my database and I will try again using
LINQ with Many to Many relationships.

Thanks,
Miguel

Oct 18 '07 #3

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

Similar topics

8
1590
by: subtle | last post by:
I've installed Orcas beta1 March 2007 CTP. I get compiler errors on any LINQ related code. For example: List<stringcities = new List<string>(); cities.Add("London"); cities.Add("Paris"); cities.Add("Las Vegas"); var holdit = from city in cities where city.Length 4 select city.ToUpper();
5
2609
by: =?Utf-8?B?Tmlrb2xheSBQb2Rrb2x6aW4=?= | last post by:
Good noon! Maybe anyone of you knows how generate LINQ Classes programmatically? Thanks in advance!
4
1952
by: Supremelink | last post by:
Hi all, I have a problem with linq in a webservice. While executing a simple query like: var result = from customer in dbConn.Customers select customer; I get this error message: "Cannot use wildcards at the top level of a
0
940
by: Lloyd Sheen | last post by:
I was in a thread talking about how to populate a listview from a Linq query. This got my curiosity up so I created a usercontrol which is a listview which can be populated from a Linq query. I has all the same properties / methods available to a listview but behind the scenes it is really a virtual listview. It is quick to load (33K items in less than a second including the query). It can be sorted and uses the dynamic linq vb...
9
2505
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 about vs2008? is it different name space or framework for linq xml or linq sql? ( 2. do we need to have references to what linq's dlls. or namespaces? system core? 3. what name spaces are needed?
1
1842
by: shapper | last post by:
Hi, On a form I have an input where tags are inserted in a CSV format. Then on my code I convert the CSV string to a List<Tag>. Tag is an object with two properties: TagID and Name So when I do the conversion I only fill the Tags names ... Then I use a join to find these tags into my database.Tags and get their ID's.
14
3691
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
4
2001
by: JS | last post by:
Using Linq to work with an SQL database has worked out well so far, however I had a problem recently where I wanted to subclass one of the auto-generated table classes. I'd like to be able to insert an instance of the subclass into the table, but an exception is thrown. I'm not at my dev computer right now, so I don't have the exception details but I wondered if anyone knew why we can't do this or what I need to do to insert an instance...
1
2345
by: shapper | last post by:
Hello, I have three tables with the following columns: Polls (PollID, Question) Options (OptionID, PollID, Answer) Votes (VoteID, OptionID) Then I have two wappers around Polls and Options, named PollPaper and OptionsPaper: public class PollPaper {
0
10459
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.