Solving Error: ‘System.Collections.Generic.List’ does not contain a definition for ‘Where’ and no extension method ‘Where’ accepting a first argument of type ‘System.Collections.Generic.List’ could be found (are you missing a using directive or an assembly reference?)

Solving Error: ‘System.Collections.Generic.List<Type>’ does not contain a definition for ‘Where’ and no extension method ‘Where’ accepting a first argument of type ‘System.Collections.Generic.List<Type>’ could be found (are you missing a using directive or an assembly reference?)

Recently I came across the above error while manipulating a collection using LINQ. I was querying the collection for a particular item and used “Where” extension method of “System.Collections.Generic.List”. Actually no extension methods were shown by the intellisense.

 Image

After investigating into the error I found that “System.Linq” namespace was not referenced by the class. So I added the reference of “System.Linq” and code begins to work and errors disappear.

 Image

After adding the reference extension methods are shown by intellisense.

 Image

Accessing Item Index in foreach binding

Accessing Item Index in foreach binding:

In many scenarios we want to use the index of current item in array. For example if we want to show the serial number along with the array item. Knockout provides a better way to access the index of items in an array, whether it is normal array or observable array.

The syntax $index / $index() provides the way to access the index of current item in the array. But its value is accessible within the context of foreach block. $index is an observable and is updated whenever the index of the item changes.

Index

Item

value of $index for each Item in foreach binding

0

Item1

$index / $index()  = 0

1

Item2

$index / $index()  = 1

2

Item3

$index  / $index() = 2

3

Item4

$index  / $index()  = 3

4

Item5

$index  / $index() = 4

 

For example: If we want to show the employee name with serial number (index number).

ViewModel

<script type=”text/javascript”>

function viewModelEmployee() {

            this.Employees = ko.observableArray([

{ Name: “Shiv”, Code: “001” },

{ Name: “DJ”, Code: “002” },

{ Name: “Rajneesh Rai”, Code: “003”}

]);

}

var vmEmp = new viewModelEmployee();

        $(function () {

            ko.applyBindings(vmEmp);

        });

</script>

 View

<table border=”1″>

<thead>

            <tr>

                <td>

                    Sr#

                </td>

                <td>

                    Employee Name

                </td>

            </tr>

</thead>

<tbody data-bind=”foreach: Employees”>

                <tr>

                    <td data-bind=”text : $index”>

                    </td>

                    <td data-bind=”text:Name”>

                    </td>

                </tr>

 </tbody>

 </table>

Image

 But the received result is not practical, as serial number must start with 1. To achieve this we would use $index() + 1

<table border=”1″>

            <thead>

                <tr>

                    <td>

                        Sr#

                    </td>

                    <td>

                        Employee Name

                    </td>

                </tr>

            </thead>

            <tbody data-bind=”foreach: Employees”>

                <tr>

                    <td data-bind=”text : $index() + 1″>

                    </td>

                    <td data-bind=”text:Name”>

                    </td>

                </tr>

            </tbody>

  </table>

Image

 Now we achieved our result i.e. Serial number.

 Precautions:

  1. If you just want to use index then you can use either $index or $index().
  2. If you want to use index in order to append or add something to index, use $index(). Otherwise if you use it like $index + 1 then something weird happens, it does not give the intended result.

 Image

Possible use case scenario might be..

data-bind=” ‘EmpID ‘ + ($index() + 1)”

EmpID 1

DO Use

data-bind=” $index() + 1″

1

DO Use

data-bind=” ‘EmpID ‘ + ($index + 1)”

Unexpected

DON’T Use

data-bind=” $index + 1″

Unexpected

DON’T Use

 Hope you enjoyed this post.

Introduction to Knockout js..

In today’s trend of Client-Server technology, the server is becoming slim and client getting fattier day by day. For client (Web Browser, Smartphone etc.) side use, JavaScript is very popular scripting language. So many libraries are developed using JavaScript. Knockout was developed and is maintained by Steve Sanderson, a Microsoft employee.

Knockout is a JavaScript library that is used to create rich web applications. It uses MVVM (Model-View-ViewModel ) pattern. It allows Html DOM elements to bind with any data model. It is more powerful library that uses two way data binding. UI will be automatically refreshed when the underlying data model changes and data model would also be changed while associated UI is changed.

It becomes more powerful when used with JQuery, jquery has the capability of selectors, animations etc.

Main features of Knockout: 

Image

  • Declarative Binding: Easily associate DOM elements with model data using a concise, readable syntax.
  • Automatic UI refresh: When your data model’s state changes, your UI updates automatically.
  • Open Source: it is free of charge; anybody can use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of this library.
  • Dependency Tracking: it is achieved by using special type of variables called Observables. When the underlying Data Model changes, the associated UI is also get changed and vice versa.
  • Cross Browser Compatibility: Supports all mainstream browsers.
  • Light weight: it’s just 42kb in size and can be reduced to 15kb using gzip.
  • Extensible: It is Flexible to integrate with other technologies and libraries.

A developer familiar with Ruby on Rails, ASP.Net Web Pages or ASP.Net MVC technologies may use Knockout js. In another sense, you can think of KO as a general way to make UIs for editing JSON data…

On the next several posts, i will go through Knockout in detail.

.NET interview questions 6th edition (Sixth edition) – By Shivprasad Koirala

.NET interview questions 6th edition (Sixth edition) – By Shivprasad Koirala

Today i am going to share a new released book from my favorite author, inspiration.

This book contains lots of spices that a job seeker need in a short time span… so just rush to your nearest book shop…

I just loved the “top 50 Technical and non-Technical Questions”

http://www.dotnetinterviewquestions.in/article_net-interview-questions-6th-edition-sixth-edition-by-shivprasad-koirala_144.html