What's the difference between these LINQ queries ?

Posted by SnAzBaZ on Stack Overflow See other posts from Stack Overflow or by SnAzBaZ
Published on 2010-03-18T10:34:27Z Indexed on 2010/03/18 11:01 UTC
Read the original article Hit count: 247

Filed under:
|
|
|

I use LINQ-SQL as my DAL, I then have a project called DB which acts as my BLL. Various applications then access the BLL to read / write data from the SQL Database.

I have these methods in my BLL for one particular table:

    public IEnumerable<SystemSalesTaxList> Get_SystemSalesTaxList()
    {
        return from s in db.SystemSalesTaxLists
               select s;
    }

    public SystemSalesTaxList Get_SystemSalesTaxList(string strSalesTaxID)
    {
        return Get_SystemSalesTaxList().Where(s => s.SalesTaxID == strSalesTaxID).FirstOrDefault();
    }

    public SystemSalesTaxList Get_SystemSalesTaxListByZipCode(string strZipCode)
    {
        return Get_SystemSalesTaxList().Where(s => s.ZipCode == strZipCode).FirstOrDefault();
    }

All pretty straight forward I thought.

Get_SystemSalesTaxListByZipCode is always returning a null value though, even when it has a ZIP Code that exists in that table.

If I write the method like this, it returns the row I want:

    public SystemSalesTaxList Get_SystemSalesTaxListByZipCode(string strZipCode)
    {
        var salesTax = from s in db.SystemSalesTaxLists
                       where s.ZipCode == strZipCode
                       select s;

        return salesTax.FirstOrDefault();
    }

Why does the other method not return the same, as the query should be identical ?

Note that, the overloaded Get_SystemSalesTaxList(string strSalesTaxID) returns a record just fine when I give it a valid SalesTaxID.

Is there a more efficient way to write these "helper" type classes ?

Thanks!

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about c#