Preamble:
I’m trying to learn C#/LINQ by creating a new application from an existing program. Essentially, I’m porting SQL stored procedures to C#/LINQ code with a new database and slightly different functionality. Although I’ve had assistance with getting going I’ve ran into my first what some might say trivial problem.
Although any correct answer would be very much appreciated so would direction. (e.g. is it just a query with simply a Group Into clause? have I missed a similar question in stackoverflow? I have lots of learning tools at my disposal thanks to browsing stackoverflow for a number of months, 101 LINQ Samples, C# In Depth, C# 4.0 in a Nutshell and LINQPad. The information is great, it’s just that I’m a bit overwhelmed by it.
Question:
I have the following table:
CREATE TABLE [dbo].[User]
([Id] [nvarchar](255) NOT NULL,
[Name] [nvarchar](max) NOT NULL,
[LineManagerId] [nvarchar](255) NULL,
[Email] [nvarchar](max) NOT NULL,
[IsAdmin] [bit] NULL,
)
The primary key is Id, the LineManagerId is a foreign key of Id.
I would like to would like to write LINQ code to query the database to find if an Id is a LineManagerId AND if so select all the Ids that that LineMangerId has?
I’ve had an attempt as follows but am missing some what I reckon are very basic skills in my understanding of LINQ.
var ismanager = from manager in Users
join man in Users on manager.Id equals man.LineManagerId
select man.LineManagerId;
Thanks for reading.
Taken literally that’s two separate queries, but here’s one that will give you all of the users under a line manager: