Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
SolvedPrivate

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

Posted on 2014-04-18
7
Medium Priority
?
78 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
 
LVL 1

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 1

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 400 total points
ID: 40010021
HI,

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

Above may Helpful
Thanks
Deepu
0
 
LVL 1

Author Closing Comment

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

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
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.

648 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