Is it possible to use derived tables in LINQ 2 SQL?

Is it possible to get something like the following in LINQ 2 SQL?

SELECT e.A, d.B
FROM
(
      SELECT * FROM Event
      WHERE ID BETWEEN 1 AND 21
) e
LEFT OUTER JOIN EventDetail d ON e.ID = d.EventID
LVL 3
Mike_MozhaevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris MConsulting - Technology ServicesCommented:
Yes. You can create a view as follows:

CREATE VIEW MY_VIEW AS
SELECT e.A, d.B
FROM
(
      SELECT * FROM Event
      WHERE ID BETWEEN 1 AND 21
) e
LEFT OUTER JOIN EventDetail d ON e.ID = d.EventID

Then go to linq and simply run SELECT * FROM MY_VIEW.

Cool stuff.
0
Mike_MozhaevAuthor Commented:
Thanks, that will work.
But I'm looking for way to express it in LINQ without any SQL since this way I have strong typing.
0
Fernando SotoRetiredCommented:
Hi Mike;

Try it like this.

DataClasses1DataContext db = new DataClasses1DataContext();

var results = from ev in db.Event
              where ev.ID >= 1 & ev.ID <= 21
              join ed in db.EventDetail on ev.ID equals ed.EventID into events
              from event in events.DefaultIfEmpty()
              select new
              {
                  ev,
                  event
              };

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike_MozhaevAuthor Commented:
It works, though without derived table. But I haven't found any other way so I'll use this approach.
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.