Link to home
Start Free TrialLog in
Avatar of veruthandai
veruthandaiFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of williamcampbell
williamcampbell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Probably ElementId is an integer already in your database.
newKey.ElementID = Int32.Parse(elementid.ToString());
should become
newKey.ElementID = elementid.Single();
Avatar of veruthandai

ASKER

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.