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.


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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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.
Hans_ChristianAuthor Commented:
There must be someone out there able to translate 5 lines of SQL code to Linq C-sharp?!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Group By:

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

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

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

From novice to tech pro — start learning today.