简体   繁体   中英

DistinctBy not working in C#/ Visual Studio 2012

I am using DistinctBy to return a distinct list. It is somehow not accepting the method.

List<LastLocation> tempLast = details.DistinctBy(x => x.deviceID).ToList();

This was a working project a few days ago when I was using Visual Studio 2008. I migrated the project to Framework 4.0 and I am currently using Visual Studio 2012 and suddenly this has started causing problems. Below is the list of Namespaces being used and assemblies referenced.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COMMON;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;
using System.Xml.Linq;

Am I doing anything wrong?

Ok this is a little late, however you do not need to install any NUGET package to handle DistinctBy requests. Simply adding the below line of code will point it at one of the many pre-installed references.

using Microsoft.Ajax.Utilities;

Once this is added, you can simply put .DistinctBy(d => d.Name) at the end of any returned collection, similar to the below example.

var someVariableName = _repositoryName.GetListOfData().DistinctBy(d => d.Name).ToList();

DistincyBy is provided through MoreLinq and not part of the framework. Check you've got MoreLinq installed and referenced.

DistinctBy is not a part of Linq. Maybe you are missing a NuGet package for MoreLinq or you did not include your own extension methods.

尝试这个:

details.Select(x => x.deviceID).Distinct().ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM