SolvedPrivate

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

Posted on 2014-04-18
7
68 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 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