Solved

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

Posted on 2009-06-30
3
557 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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