Solved

Converting from linq.iqurable

Posted on 2014-07-24
2
118 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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now