Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 134
  • Last Modified:

Converting from linq.iqurable

I have a class

public class Document
    {
        public int Doc_ID { get; set; }
        public string personid { get; set; }
        public string DocType { get; set; }
        public byte[] uploaded_Document { get; set; }
        public string Document_Name { get; set; }
    }

Document UserDoc = new Document();

UserDoc= from uDoc in db.userDoc where uDoc.personid == "X123456" select uDoc;


how to solve this casting problem ?
0
Vikash p
Asked:
Vikash p
2 Solutions
 
Fernando SotoCommented:
Hi Vikash;

I am assuming that the class UserDoc in your code matches the fields that you want to retrieve from the table in the database and that UserDoc is not part of the data model that was created by the ORM. A query returns a collection of object and therefore to retrieve one record you need to use something like the FirstOrDefault as shown in the query below.
UserDoc = (from uDoc in db.userDoc 
           where uDoc.personid == "X123456" 
           select new Document
           {
               Doc_ID = uDoc.Doc_ID,
               personid = uDoc.personid,
               DocType = uDoc.DocType,
               uploaded_Document = uDoc.uploaded_Document,
               Document_Name = uDoc.Document_Name
           }).FirstOrDefault();

Open in new window

0
 
Lokesh B RDeveloperCommented:
Hi,

try this

Document UserDoc = new Document();

UserDoc =  db.userDoc.where(c=> c.personid == "X123456").FirstOrDefault();

if(UserDoc != null)
{
     int Id = UserDoc.Doc_ID; 

}

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now