SolvedPrivate

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

Posted on 2014-04-18
7
70 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
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
Independent Software Vendors: 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 47
sql update 2 35
MS Access Pasthru Query to Execute SQL Stored procedure 8 26
Convert VB web project to C# 3 32
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

685 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