veruthandai
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.
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());
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
var elementid = Database.Elements.Single<E lement>(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.
I was wondering, can you explain what is going on here? Specifically what the => is doing? I'm unfamiliar with this operator.
newKey.ElementID = Int32.Parse(elementid.ToSt
should become
newKey.ElementID = elementid.Single();