LINQ insert or query from foreign key

Posted on 2009-05-01
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)
- ElementId
- ElementName

- KeyId
- KeyValue
- ElementId
- CollectionId

- 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

Question by:veruthandai
    LVL 12

    Accepted Solution

    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

    LVL 14

    Expert Comment

    Probably ElementId is an integer already in your database.
    newKey.ElementID = Int32.Parse(elementid.ToString());
    should become
    newKey.ElementID = elementid.Single();

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    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 documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now