To obtain the query you want you need to call first a Select
before call Distinct
to get only the columns you need to apply a distinct:
var data = context.TableX.Where(w => w.userId == 9999&& w.id == 9999)
.Select(e=>new {e.id, e.name, e.companyId})
.Distinct()
.ToList();
Update 1
I'm pretty sure the first query should work but anyways another solution could be applying a group by:
var data = context.TableX.Where(w => w.userId == 9999&& w.id == 9999)
.GroupBy(e=>new {e.id, e.name, e.companyId})
.Select(g=>new{g.Key.id, g.Key.name, g.Key.companyId})
.ToList();
Update 2
Now I tested the first query in LinqPad in another context but using the same idea:
var query=Agencies.Where(a=>a.StatusId==1)
.Select(e=>new{e.StateId, e.AgencyName})
.Distinct()
.Dump();
And this is the sql that was generated:
-- Region Parameters
DECLARE @p0 Int = 1
-- EndRegion
SELECT DISTINCT [t0].[stateId] AS [StateId], [t0].[agencyName] AS [AgencyName]
FROM [Agencies] AS [t0]
WHERE [t0].[statusId] = @p0
As you can see, it should work, I don't really know what is happening in your case.
Repeating the same process to the second query to be executed via LinqPad:
var query=Agencies.Where(a=>a.StatusId==1)
.GroupBy(e=>new{e.StateId, e.AgencyName})
.Select(g=>new{g.Key.StateId, g.Key.AgencyName})
.Dump();
And this is the sql code:
-- Region Parameters
DECLARE @p0 Int = 1
-- EndRegion
SELECT [t0].[stateId] AS [StateId], [t0].[agencyName] AS [AgencyName]
FROM [Agencies] AS [t0]
WHERE [t0].[statusId] = @p0
GROUP BY [t0].[stateId], [t0].[agencyName]