Solved

Linq cast a table as a custom class

Posted on 2013-11-14
1
227 Views
Last Modified: 2016-02-10
I created a class that matches a database table that I have in SQL. How can cast my var as this new custom class.

Person C = (from x in ctx1.Persons where x.PersonID == RecID select x).FirstOrDefault();
I want to dump this into a custom class name PersonInfo.
0
Comment
Question by:NickMalloy
1 Comment
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 350 total points
Comment Utility
Person C = from x in ctx1.Persons
             where x.PersonID == RecID
             select new PersonInfo()
             {
                 PersonID = x.PersonID,
                 PersonName = x.PersonName,
                 // etc
              }
             .FirstOrDefault();

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

8 Experts available now in Live!

Get 1:1 Help Now