the following code for some bizarre reason gives me a value for each row in the table. I just want unique values :
public IQueryable<WasteIndicatorItem> FindAllEWC()
{
var results = (from x in this._db.WasteIndicatorItems
orderby x.EWC
select x).ToList();
return results.OrderBy(s => s.EWC).Distinct().AsQueryable();
}
I’ve tried moving the .Distinct() all over the place, before the ToList ETC. What am i doing wrong must be something simple, cheers for reading..
So in my results I get :
13
13
13
12345
12345
rather than :
13
12345
TABLE STRUCTURE IS :
CREATE TABLE [dbo].[WasteIndicatorItem](
[WasteIndicatorItemId] [int] IDENTITY(1,1) NOT NULL,
[WasteId] [int] NOT NULL,
[WasteIndicatorId] [int] NOT NULL,
[EWC] [varchar](6) COLLATE Latin1_General_CI_AS NOT NULL,
[WasteHazardCodeId] [int] NOT NULL,
[Quantity] [money] NOT NULL,
[Notes] [varchar](4000) COLLATE Latin1_General_CI_AS NULL,
[EnteredById] [int] NOT NULL,
[Entered] [datetime] NOT NULL,
[LastModifiedById] [int] NULL,
[LastModified] [datetime] NULL,
CONSTRAINT [PK_IndicatorItem] PRIMARY KEY CLUSTERED
(
[WasteIndicatorItemId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
You have two choices:
GroupByand keep everything on the SQL side (preferred). You would group by the property you’re interested in that defines uniqueness to you.AsEnumerableto bring everything down, then use the overloadedDistinctmethod that accepts anIEqualityComparer. This usually isn’t a good idea (unless the result set is small) since all the matching results are returned, and less processing is done on the SQL side.Implementing the first solution looks like this: