[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

LINQ insert or query from foreign key

Posted on 2009-05-01
3
Medium Priority
?
1,184 Views
Last Modified: 2013-11-11
Alright, so here is the basic setup. I just want to figure out if I'm missing a huge piece before I keep going. I have two tables (three, truthfully)
Elements
- ElementId
- ElementName

Keys
- KeyId
- KeyValue
- ElementId
- CollectionId

Tags
- TagId
- ElementId
- CollectionId

Elements is a table to store the labels for both keys and tags. ElementName is basically a text field. Both Tags and Keys reference the ElementId to make sure that there are no duplicates of any entry.

Below is a sample of the way I'm doing linq code to try and get this data and use it. I was wondering if there was a cleaner, more logical solution in LINQ.
Key newKey = new Key();
 
			var elementid = from i in Database.Elements
							   where i.Name == "Name"
							   select i.ElementId;
 
			newKey.ElementId = Int32.Parse(elementid.ToString());

Open in new window

0
Comment
Question by:veruthandai
3 Comments
 
LVL 12

Accepted Solution

by:
williamcampbell earned 2000 total points
ID: 24282393
Looks ok may select multiple elements if there are duplicate names

The code below will throw an exception if there are more than one results....

var elementid = Database.Elements.Single<Element>(e => e.ElementName == thename);

Open in new window

0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 24285266
Probably ElementId is an integer already in your database.
newKey.ElementID = Int32.Parse(elementid.ToString());
should become
newKey.ElementID = elementid.Single();
0
 

Author Comment

by:veruthandai
ID: 24291106
var elementid = Database.Elements.Single<Element>(e => e.ElementName == thename);
I was wondering, can you explain what is going on here? Specifically what the => is doing? I'm unfamiliar with this operator.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question