How do I transfer this code from SQL to Linq in C sharp?

I have really tried hard to get it working but I cant find the correct way to implement it in c sharp by using Linq.


SQL-Code:

SELECT Aktie.Papirnavn, Portfolio.Antal, Dagskurs.Dagskurs, Sum([Antal]*[Dagskurs]) AS Værdi
FROM (Aktie INNER JOIN Portfolio ON Aktie.Fondskode = Portfolio.Fondskode) INNER JOIN Dagskurs ON Portfolio.Fondskode = Dagskurs.Fondskode
GROUP BY Aktie.Papirnavn, Portfolio.Antal, Dagskurs.Dagskurs, Portfolio.PortfID
HAVING (((Portfolio.PortfID)="Forsigtig"));

I tried to implement some but when I try to "translate" from the pieces I read on different sites I just get tons of errors I cant understand. I got this working in a buttonclick:


            DataClasses1DataContext xxx = new DataClasses1DataContext();
            var pa = from Aktie in xxx.Akties
            join Portfolio in xxx.Portfolios on Aktie.Fondskode equals Portfolio.Fondskode
            select new {Aktie.Papirnavn, Portfolio.Antal};

            dataGridView1.DataSource = pa;



But when I try to get it more advanced i get stuck.
Hans_ChristianAsked:
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.

SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
0
Hans_ChristianAuthor Commented:
Thanks for your suggestion. However  that didnt work. Program says:

SQL cannot be converted to LINQ: SQL with sub join cannot be converted to LINQ??

This is getting annoying.
0
Hans_ChristianAuthor Commented:
There must be someone out there able to translate 5 lines of SQL code to Linq C-sharp?!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bob LearnedCommented:
SQL to LINQ is not always an easy transition.  You need to combine multiple samples to get what you need.

Group By:

http://msdn.microsoft.com/en-us/vcsharp/aa336747#maxGrouped

List<Product> products = GetProductList();
 
    var categories =
        from p in products
        group p by p.Category into g
        select new { Category = g.Key, MostExpensivePrice = g.Max(p => p.UnitPrice) };

Open in new window


Inner Join

List<Product> products = GetProductList();
 
    var soldOutProducts =
        from p in products
        where p.UnitsInStock == 0
        select p;

Open in new window


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
Hans_ChristianAuthor Commented:
The Learned One: Thanks for your response. Havent been able to respond yet. I havent tried your solution but ill look at it soon. I found a way to do it my self also that works:


DataClasses1DataContext xxx = new DataClasses1DataContext();
            var yy = from Aktie in xxx.Akties
                     join Portfolio in xxx.Portfolios
                         on Aktie.Fondskode equals Portfolio.Fondskode into Portfolios
                     from Portfolio in Portfolios
                     where Portfolio.PortfID == comboBox1.Text 
                     join Dagskur in xxx.Dagskurs on Portfolio.Fondskode equals Dagskur.Fondskode
                     select new { Aktie.Papirnavn, Portfolio.Antal, Dagskur.Dagskurs, Værdi = Portfolio.Antal  * Convert.ToInt32(Dagskur.Dagskurs)};

Open in new window

0
Hans_ChristianAuthor Commented:
Havent tried it.
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.