How to work with the Bootstrap DropDown in AngularJS

MyApp.controller(‘SubjectDropDownController’, function ($scope) {

$scope.subjects = [‘Math’, ‘Physics’, ‘Chemistry’, ‘Hindi’, ‘English’];


$scope.dropboxitemselected = function (item) {

$scope.selectedItem = item;




And the drop down can be modified to bind the selected item as shown in the listing below:

LINQ: Case insensitive group on multiple columns

You can pass StringComparer.InvariantCultureIgnoreCase to the GroupBy extension method.

var result = source.GroupBy(a => new { a.Column1, a.Column2 }, 

Or you can use ToUpperInvariant on each field as suggested by Hamlet Hakobyan on comment. I recommend ToUpperInvariant or ToUpper rather than ToLower or ToLowerInvariant because it is optimized for programmatic comparison purpose.

The type arguments for method ‘System.Linq.Enumerable.GroupBy(System.Collections.Generic.IEnumerable, System.Func, System.Collections.Generic.IEqualityComparer)’ cannot be inferred from the usage. Try specifying the type arguments explicitly.

To make it work, I found it easiest and clearest to define a new class to store my key columns (GroupKey), then a separate class that implements IEqualityComparer (KeyComparer). I can then call

var result= source.GroupBy(r => new GroupKey(r), new KeyComparer());

The KeyComparer class does compare the strings with the InvariantCultureIgnoreCase comparer, so kudos to NaveenBhat for pointing me in the right direction.

Simplified versions of my classes:

private class GroupKey
    public string Column1{ get; set; }
    public string Column2{ get; set; }

    public GroupKey(SourceObject r) {
        this.Column1 = r.Column1;
        this.Column2 = r.Column2;

private class KeyComparer: IEqualityComparer<GroupKey>

    bool IEqualityComparer<GroupKey>.Equals(GroupKey x, GroupKey y)
        if (!x.Column1.Equals(y.Column1,StringComparer.InvariantCultureIgnoreCase) return false;
        if (!x.Column2.Equals(y.Column2,StringComparer.InvariantCultureIgnoreCase) return false;
        return true;
        //my actual code is more complex than this, more columns to compare
        //and handles null strings, but you get the idea.

    int IEqualityComparer<GroupKey>.GetHashCode(GroupKey obj)
        return 0.GetHashCode() ; // forces calling Equals
        //Note, it would be more efficient to do something like
        //string hcode = Column1.ToLower() + Column2.ToLower();
        //return hcode.GetHashCode();
        //but my object is more complex than this simplified example


Source: stackoverflow

Gets the Description attribute from an Enum

public static class EnumEx
    public static T GetValueFromDescription<T>(string description)
        var type = typeof(T);
        if(!type.IsEnum) throw new InvalidOperationException();
        foreach(var field in type.GetFields())
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;
            if(attribute != null)
                if(attribute.Description == description)
                    return (T)field.GetValue(null);
                if(field.Name == description)
                    return (T)field.GetValue(null);
        throw new ArgumentException("Not found.", "description");
        // or return default(T);


var panda = EnumEx.GetValueFromDescription<Animal>("Giant Panda");

source: stackoverflow

Func vs. Action vs. Predicate Delegates

  • Predicate: essentially Func<T, bool>; asks the question “does the specified argument satisfy the condition represented by the delegate?” Used in things like List.FindAll.
  • Action: Perform an action given the arguments. Very general purpose. Not used much in LINQ as it implies side-effects, basically.
  • Func: Used extensively in LINQ, usually to transform the argument, e.g. by projecting a complex structure to one property.

Other important delegates:

  • EventHandler/EventHandler<T>: Used all over WinForms
  • Comparison<T>: Like IComparer<T> but in delegate form.


The difference between Func and Action is simply whether you want the delegate to return a value (use Func) or not (use Action).

Func is probably most commonly used in LINQ – for example in projections:

 list.Select(x => x.SomeProperty)

or filtering:

 list.Where(x => x.SomeValue == someOtherValue)

or key selection:

 list.Join(otherList, x => x.FirstKey, y => y.SecondKey, ...)

Action is more commonly used for things like List<T>.ForEach: execute the given action for each item in the list. I use this less often than Func, although I do sometimes use the parameterless version for things like Control.BeginInvoke and Dispatcher.BeginInvoke.

Predicate is just a special cased Func<T, bool> really, introduced before all of the Func and most of the Action delegates came along. I suspect that if we’d already had Func and Action in their various guises, Predicate wouldn’t have been introduced… although it does impart a certain meaning to the use of the delegate, whereas Func and Action are used for widely disparate purposes.

Predicate is mostly used in List<T> for methods like FindAll and RemoveAll.


source: stackoverflow

Selecting, Grouping on child list -> Single statement requested

This gets you a list of objects, each containing one item and a distinct list of customers who bought it:

var items =
   .SelectMany(c => c.Orders.SelectMany(
      o => o.Items.Select(i => new { item = i, customer = c })
   .GroupBy(o => o.item.ItemCode)
   .Select(g => new {
      item = g.First().item,
      customers = g.Select(o => o.customer).Distinct()

Test data:

Customer[] customers = {
   new Customer("John Doe", new Order("A", "B")),
   new Customer("Jane Doe", new Order("B", "C", "D"), new Order("B", "D")),
   new Customer("Ford Prefect", new Order("D"), new Order("A", "E"))


A: John Doe, Ford Prefect
B: John Doe, Jane Doe
C: Jane Doe
D: Jane Doe, Ford Prefect
E: Ford Prefect

source :

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.