C# LINQ find duplicates in List

The easiest way to solve the problem is to group the elements based on their value, and then pick a representative of the group if there are more than one element in the group. In linq, this translates to:

var query = lst.GroupBy(x=>x)

If you want to know how many times the elements are repeated, you can use:

var query = lst.GroupBy(x=>x)
              .Select(y=> new { Element = y.Key, Counter = y.Count()})

This will return a List of an anonymous type, and each element will have the properties Element and Counter, to retrieve the informations you need.

And lastly, if it’s a dictionary you are looking for, you can use

var query = lst.GroupBy(x=>x)

This will return a dictionary, with your element as key, and the number of times it’s repeated as value.


source: http://stackoverflow.com/questions/18547354/c-sharp-linq-find-duplicates-in-list

RegisterType with a PARAMETER constructor

class ThreadPool
   public ThreadPool(int maxThreads, ILogger logger)
container.RegisterType<ThreadPool>(new InjectionConstructor(20,typeof(ILogger)));

I suppose using typeof in this scenario makes some sense, but it’s hardly intuitive.  The API could be improved so that I don’t need to specify anything about ILogger resolution at all.   I should only have to specify explicitly the constructor arguments that cannot be resolved automatically, e.g.:

container.RegisterType<ThreadPool>().WithConstructorArg("maxThreads", 20);


Using LINQ to remove elements from a List

Well, it would be easier to exclude them in the first place:

authorsList = authorsList.Where(x => x.FirstName != "Bob").ToList();

However, that would just change the value of authorsList instead of removing the authors from the previous collection. Alternatively, you can use RemoveAll:

authorsList.RemoveAll(x => x.FirstName == "Bob");

If you really need to do it based on another collection, I’d use a HashSet, RemoveAll and Contains:

var setToRemove = new HashSet<Author>(authors);
authorsList.RemoveAll(x => setToRemove.Contains(x));

How to convert DataTable to class Object

Initialize DataTable:

DataTable dt = new DataTable(); 
dt.Columns.Add("id", typeof(String)); 
dt.Columns.Add("name", typeof(String)); 
for (int i = 0; i < 5; i++)
    string index = i.ToString();
    dt.Rows.Add(new object[] { index, "name" + index });

Query itself:

IList<Class1> items = dt.AsEnumerable().Select(row => 
    new Class1
            id = row.Field<string>("id"),
            name = row.Field<string>("name")



GROUP BY to combine/concat a column

Given the  table as follow:

ID  User  Activity  PageURL  
 1  Me    act1      ab     
 2  Me    act1      cd     
 3  You   act2      xy     
 4  You   act2      st

I want to group by User and Activity such that I end up with something like:

User  Activity  PageURL  
Me    act1      ab, cd     
You   act2      xy, st
     [User], Activity,
         (SELECT DISTINCT ',' + PageURL
          FROM TableName
          WHERE [User] = a.[User] AND Activity = a.Activity
          FOR XML PATH (''))
          , 1, 1, '')  AS URLList
FROM TableName AS a
GROUP BY [User], Activity

LINQ: dot notation equivalent for JOIN

Consider this LINQ expression written using query notation:

List<Person> pr = (from p in db.Persons
                     join e in db.PersonExceptions
                     on p.ID equals e.PersonID
                     where e.CreatedOn >= fromDate
                     orderby e.CreatedOn descending
                     select p)

Question: how would you write this LINQ expression using dot notation?

List<Person> pr = db.Persons
                          p => p.ID,
                          e => e.PersonID,
                          (p, e) => new { p, e })
                    .Where(z => z.e.CreatedOn >= fromDate)
                    .OrderByDescending(z => z.e.CreatedOn)
                    .Select(z => z.p)

Note how a new anonymous type is introduced to carry both the p and e bits 
forward. In the specification, query operators which do this use transparent
identifiers to indicate the behaviour.

source: http://stackoverflow.com/questions/1511833/linq-dot-notation-equivalent-for-join