SolvedPrivate

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

Posted on 2014-04-18
7
66 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:FaheemAhmadGul
Comment Utility
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
Comment Utility
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
Comment Utility
HI,

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

Above may Helpful
Thanks
Deepu
0
 

Author Closing Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Check if number is currency 15 27
Image(7) 1 32
What .NET website keeps me current? 9 28
Convert int to military time 8 20
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

762 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