Solved

Converting from linq.iqurable

Posted on 2014-07-24
2
121 Views
Last Modified: 2016-02-18
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
Comment
Question by:Vikash p
2 Comments
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40218049
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
 
LVL 14

Accepted Solution

by:
Lokesh B R earned 250 total points
ID: 40620858
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net WebRequest Code from JSON curl 7 54
Groupbox Control ? 2 19
C# Linq Select From List 3 18
Coding for the first time 9 36
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

820 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