Linq query for Asp.net

System.Linq.IQueryable<CustomerProfile> query = 
    from usr in context.customer_profiles
    where usr.cust_email == LoginID
    select new CustomerProfile
    {
        NAME = usr.cust_name,
        CONTACT = usr.cust_contact,
        EMAILID = usr.cust_email,
        LOCATION = usr.cust_location,
        SERVICELOCATION=usr.cust_service_location,
        FAXNO=usr.cust_fax_no,
        FIRMNAME = usr.cust_firm_name,
        FIRMADDRESS = usr.cust_firm_address,
        DATEESTABLISHED = Convert.ToDateTime(((DateTime?)usr.date_of_established)),
        SIGNATURE = usr.cust_signature,
        LOGO =  usr.logo,
    };

On the next line, I get a message that "the specified cast is invalid." How can i fix this?

return query.ToList().FirstOrDefault(); 
+3
source share
2 answers

You are comparing customer email with the login ID of your request. I assume that this will cause a casting problem if it does not return the correct results.

And use implicit listing (var), not IQueryable

0
source

Try this and then return the request. Sorry for the formatting, this gave me the blues.

        CustomerProfile query =
         (from usr in context.customer_profiles
          where usr.cust_email == LoginID
          select new
          {
              NAME = usr.cust_name,
              CONTACT = usr.cust_contact,
              EMAILID = usr.cust_email,
              LOCATION = usr.cust_location,
              SERVICELOCATION = usr.cust_service_location,
              FAXNO = usr.cust_fax_no,
              FIRMNAME = usr.cust_firm_name,
              FIRMADDRESS = usr.cust_firm_address,
              DATEESTABLISHED = Convert.ToDateTime(((DateTime?)usr.date_of_established)),
              SIGNATURE = usr.cust_signature,
              LOGO = usr.logo,
          }).FirstOrDefault();
0
source

All Articles