1

I have a difficulty in parsing user input to a variable in an SQL sproc. I'm using linq as a go-between.

My web form contains, amongst other things, a textbox for a user to input a search parameter. This content has to be parsed from Text to a string, and subsequently to a char of 16 length. The data in the table that I am looking at is a char of the same length.

So, I tried the following:

public void GetASingleInvoice(string invoiceNumber)
    {
        char iNum = Convert.ToChar(invoiceNumber);

        var invoice = from SIMS_InvoiceNo in financedb.INVOICEs
                      where SIMS_InvoiceNo = iNum
                      select SIMS_InvoiceNo;
    }

but iNum at line 6 has a beautiful squiggly line, telling me I cannot convert type "char" to
"(corporatepath).Models.INVOICE".

Have I broken linq?
I have the class for invoice here: http://pastebin.com/da6PJxn6

Edit: I have also tried the double operator (==) which results in an error specifying that a single operator is to be used here.

Note: Changing the type of the column in SQL is not an option.

1
  • 2
    It's funny to see how all copy-paste ` = iNum` in their answers :) Commented May 29, 2014 at 11:35

3 Answers 3

2

You need to specify the property of the INVOICE class like this:

var invoices = from invoice in financedb.INVOICEs
              where invoice.SIMS_InvoiceNo == iNum
              select invoice;
Sign up to request clarification or add additional context in comments.

1 Comment

If I use this, it results in a definition error - INVOICE does not contain a definition for invoiceNumber. Do you mean to imply that I should put a definition for invoiceNumber in ~.Invoice?
1

you have missed to provide the column:

 var invoice = from Invoice in financedb.INVOICEs
         ----> where Invoice.SomeProperty == iNum
               select Invoice;

Also in linq where you need == not =

Comments

0

Correct query:

var invoices = from i in financedb.INVOICEs
               where i.SIMS_InvoiceNo == iNum
               select i;

Or if you want single invoice by number, then simply use FirstOrDefault or SingleOrDefault methods:

var invoice = financedb.INVOICEs.FirstOrDefault(i => i.SIMS_InvoiceNo == iNum);

2 Comments

@RoteKatze btw if you'll switch to Entity Framework (which I suggest to do, if its possible), there is even more handy method Find, and getting entity by key looks like financedb.INVOICEs.Find(iNum)
I plan to with future projects, but this system is being decommissioned soon. This is only a small intranet program to visually compare finances :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.