SolvedPrivate

Help writing a C# function which returns the ID of a particular record from a table

Posted on 2014-04-18
7
71 Views
Last Modified: 2016-02-16
I have a table named (BookNames) in an SQL Database with just one field named BookTitle which stores strings.
I need help writing a C# Function using LINQ which takes a string parameter and returns the ID of a record from that table that matches the string passed as a paremeter to this function.
I am using EntityFramework in my application and have a DBContext name BooksDBContext in my application.

So if my table with its data looks like as below:

ID  BookTitle
1   War and Peace
2   A Farewell to Arms
3   Emma

And I pass a string (bookTitle "A Farewell to Arms") to the following function:

int MyFuntionToGetID (string bookTitle)
{

BooksDBContext context = new BooksDBContext();

//code here

return ID;
}

This function should return me 2 which is the ID of the second record in this table.
0
Comment
Question by:FaheemAhmadGul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40009410
I need help writing a C# Function using LINQ...
Using LINQ in what context? Are you using either Entity Framework or LINQ-to-SQL, or are you planning on using LINQ after the fact, when the data is already in your application (a.k.a. LINQ-to-Objects)?
0
 

Author Comment

by:FaheemAhmadGul
ID: 40009468
Thank you for your prompt response to my request for help. It will be LINQ to entities. The data is already in my application. The DBContext is named BooksDbContext.
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40009522
try something like this


public void Getbookid(<BOOK NAME VARIABLE HERE>)
{
    DataClasses1DataContext dc = new DataClasses1DataContext();

    var query = (from booktable in BooksDBContext.GetTable<table>()
                 select orders);

    var matches = (from c in query
                   where c.BookTitle == "<VARIABLE HERE>"
                   select
                   c.BOOKTABLE.ID).SingleOrDefault<System.String>();

    MessageBox.Show(matches);

}




http://www.codeproject.com/Articles/26657/Simple-LINQ-to-SQL-in-C
--example7
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FaheemAhmadGul
ID: 40009593
I do not get any option such as GetTable on typing BooksDBContext.
The link provided for the codeproject is helpful though. I am studying it but I would appreciate a more specific answer to my problem if possible.
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 40009630
try this
in your //CODE HERE part



string ID = (from c in context.Customers

                 where c.BookTitle.Equals(bookTitle)

                 select c.ID).AsEnumerable().DefaultIfEmpty("[MISSING]").Single();
0
 
LVL 8

Accepted Solution

by:
deepu chandran earned 100 total points
ID: 40010021
HI,

var CustID= BooksDBContext.Customers
                       .Where(p => p.BookTitle.Contains(BookTitle))
                       .Single()
                       .ID;

Above may Helpful
Thanks
Deepu
0
 

Author Closing Comment

by:FaheemAhmadGul
ID: 40010033
This has worked. Many thanks. I am very grateful. Regards - Faheem
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Analysis 7 58
SQL - Load records to temp table through CTE 6 37
invoke-sqlcmd help 5 29
Delete duplicates from SQL Server table 2 24
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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

737 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