LINQ insert or query from foreign key

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

veruthandaiAsked:
Who is Participating?
 
williamcampbellCommented:
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
 
PockyMasterCommented:
Probably ElementId is an integer already in your database.
newKey.ElementID = Int32.Parse(elementid.ToString());
should become
newKey.ElementID = elementid.Single();
0
 
veruthandaiAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.