Solved

Create a non-anonymous "select new" in LINQ to SQL

Posted on 2009-06-30
3
547 Views
Last Modified: 2013-11-11
This is really a two part question,

1) How do you do an "AND" clause in a join (see code snippet)

2) How do I take my "select new" results and turn it so I can return it as either a non-anonymous type or return it as a list.
0
Comment
Question by:bryan_z
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 125 total points
ID: 24748641
Hi bryan_z;

Not sure if this is what you are asking for question 1 if not post your example so that I can have a better idea, see code snippet below.

In answer to 2 you create a class with public properties with the names of the variables in the select statement, see code snippet 2 below.

Fernando
DataClasses1DataContext db = new DataClasses1DataContext();
 

// Query was copied from Microsoft Documentation with small modification

List<MyResults> query = (from o in db.Orders

                         from p in db.Products

                         join d in db.Order_Details

                         on new { o.OrderID, p.ProductID } equals new

                         {

                             d.OrderID,

                             d.ProductID

                         } into details

                         from d in details

                         select new MyResults

                         {

                             OrderID = o.OrderID,

                             ProductID = p.ProductID,

                             UnitPrice = d.UnitPrice

                         }).ToList();
 

foreach (MyResults mr in query)

{

    Console.WriteLine("OID = {0}  PID = {1}  UC = {2}",

        mr.OrderID, mr.ProductID, mr.UnitPrice);

}
 
 

public class MyResults

{

    public int OrderID { get; set;  }

    public int ProductID { get; set; }

    public decimal UnitPrice { get; set; }

}

Open in new window

0
 

Author Closing Comment

by:bryan_z
ID: 31598461
Thanks. I thought i had posted the code example. Your example was great and just what I was looking for
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 24749203
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

12 Experts available now in Live!

Get 1:1 Help Now