?
Solved

Converting from linq.iqurable

Posted on 2014-07-24
2
Medium Priority
?
127 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 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 15

Accepted Solution

by:
Lokesh B R earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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