0

Lately im working on an C# Project and I am trying to do a difficult SQL Statement in LINQ C#. But i have no idea how to do this.

Here is the Statement:

SELECT *
FROM members 
INNER JOIN group_has_members ON members.ID = group_has_members.members_ID 
WHERE group_has_members.group_ID != 1 
AND group_has_members.members_ID NOT IN(SELECT group_has_members.members_ID 
FROM group_has_members 
WHERE group_has_members.group_ID = 1)

Would be nice if you help me :D Yours Florian

Tim Freese
  • 435
  • 3
  • 14

3 Answers3

1

I think you need something like this, I didn't test this query but it will be something along these lines. See How would you do a "not in" query with LINQ? for some ideas.

var mem = from m in members 
          join gm in group_has_members on m.ID equals gm.members_ID
          where gm.members_ID != 1 && 
              !(from ghm in group_has_members
                where ghm.group_ID = 1
                select ghm.members_ID).Contains(gm.members_ID)
          select m;

Also a great tool for this is LinqPad, if your just starting out. You can paste the SQL into the window and view the linq equivalent.

Community
  • 1
  • 1
sgtrice1
  • 101
  • 4
1

check this converter, I hope it helps to you

http://sqltolinq.com/

0

If you currently have a query and you would reuse it in a project based on linq to SQL, you don't need to work again on the same query to get it in linq semantic. You could use Execute command to use your query directly.

https://msdn.microsoft.com/it-it/library/system.data.linq.datacontext.executecommand(v=vs.110).aspx

Skary
  • 1,322
  • 1
  • 13
  • 40