Solved

Converting from linq.iqurable

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

705 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

18 Experts available now in Live!

Get 1:1 Help Now