Friday, 6 July 2018

009 LINQ OfType


OfType method is used to filter LINQ objects.OfType comes under System.Linq  namespace. OfType is Enumarable ,  ofTypeIEnumarable. When ofType is applied on a query, the query Returns the result, that can be cast into particular supplied type.Object that cannot be cast into the supplied type will be discarded automatically.ofType a generic method, it takes type as parameter, that has to be filtered. 
 
Here is the OfType example ,the list is filtered against
String , Decimal ,Integer


Eample

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Linq;
using System.Collections;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
                  class Program
                 {
                   static void Main(string[] args)
                    {

                             IList myList = new ArrayList();

                            myList.Add("India");
                            myList.Add(false);
                            myList.Add(12);
                            myList.Add(true);
                            myList.Add(13);
                            myList.Add("USA");
                            myList.Add("Pakistan");
                           myList.Add(15);
                           myList.Add(true);
                           myList.Add("South Afrika");
                           myList.Add("Russia");



                          /********filtering string object*************/
                           var strqery = from s in myList.OfType<string>()
                                                   select s;

                             Console.WriteLine("*************List of String object*************");
                                                  foreach (object c in strqery)
                                                    {
                                                         Console.WriteLine(c.ToString());
                                                    }


                       /********filtering integer object*************/
                        var intqery = from s in myList.OfType<int>()
                                                select s;

                       Console.WriteLine("*************List of Integer object*************");
                     
                       foreach (object c in intqery)
                        {
                                 Console.WriteLine(c.ToString());
                        }


                     /********filtering decimal object*************/
                     Console.WriteLine("*************List of Decimal object*************");
                     var decqery = from s in myList.OfType<bool>()
                                              select s;

                      foreach (object c in decqery)
                     {
                               Console.WriteLine(c.ToString());
                      }

                        Console.ReadKey();
            }
      }

}

Output:
*************List of String object*************
India
USA
Pakistan
South Afrika
Russia
*************List of Integer object*************
12
13
15
*************List of Decimal object*************
False
True
True

Thursday, 5 July 2018

008 LINQ with MVC Razor


LINQ was introduced .Net Framework 3.5 (Visual Studio 2010).LINQ apply OOPS concept with relational database. MVC application  can be used with LINQ to retrieve and modify data from database and collections. You can use blank MVC Razor template for this purpose.In this example ,we have used a blank MVC Razor template and a local sql server database.


The first step is to design of view of the Project.MVC Razor uses "cshtml" file.We have added a file called "Student.cshtml" under a folder "Home". Now we have to design "cshtml" , which accept input Name,Roll,Age.

---------------------------Student.cshtml------------------------------------
@{
Layout = null;
}
 @using (Html.BeginForm("SaveCmd", "Home", FormMethod.Post, new { id = "submitForm" }))
{
        <div style="width:500px;">
        <fieldset>
        <legend>Student:</legend>
                  <table>
                         <tr>
                             <td>
                                    Name :
                             </td>
                            <td>
                                    <input id="txtName" name="txtName" />
                           </td>
                       </tr>
                      <tr>
                          <td>
                                 Roll :
                         </td>
                         <td>
                                <input id="txtRoll" name="txtRoll" />
                         </td>
                     </tr>
                    <tr>
                    <td>
                            Age :
                    </td>
                   <td>
                               <input id="txtAge" name="txtAge" />
                  </td>
                 </tr>
                  <tr>
                  <td>
                          <button type="submit" id="btnSave" name="Command" value="Save">
                          Save</button>
                 </td>
                <td>
                </td>
             </tr>
          </table>
</fieldset>
</div>
<br />
<span>List of Student</span>
<br />
<br />
<table>
                 @foreach (var student in ViewBag.student)
                {
                      <tr>
                      <td>@student.Name
                     </td>
                     <td>|
                     </td>
                     <td>@student.Roll
                     <td>|
                     </td>
                     </td>
                     <td>@student.Age
                     </td>
                      </tr>
                    }
</table>
}

---------------------------------------------------------------
The output will be see like this.

The interface can accept  name ,roll , age of a student and a save button for saving data and a for loop to show the data in tabular format.


Now from the left hand side of the project side tab , click on "Database Explorer" ,Add database connection and add new database database ,then you will find tables. We have a already designed a table , names "student".




Now from the project,right mouse click, Add-->New Item -->LINQ to SQL class. After adding LINQ to SQL class,double click on the class and drag your database table over it. A graphical representation of SQL table will be drawn over "dbml" file.



Now go to "Home" controller , create an instance of a line LINQ to SQL class file and set the connection constructor with database during the instance creation. 

LINQtoSQLDataContext db = new LINQtoSQLDataContext(you connection string)


In this project, "Save" submit will call "SaveCmd" action under "Home" controller.FormCollection will return the values of the posted files.Now create a object of the student class, fill the object with the data from the input text boxes. When click on save button , you will see ,data has been insertedto the databases tables successfully.Below is the code for insert data.


public ActionResult SaveCmd( FormCollection fc )
{
           
LINQtoSQLDataContext db = new LINQtoSQLDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\aa\tutorial.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
               string mName = fc["txtName"].ToString();
               string mRoll = fc["txtRoll"].ToString();
               string mAge = fc["txtAge"].ToString();

              student obj = new student();
              obj.Name = mName;
              obj.Roll = Convert.ToInt16(mRoll);
              obj.Age = Convert.ToInt16(mAge);

             db.students.InsertOnSubmit(obj);
             db.SubmitChanges();

            return View("~/Views/Home/Student.cshtml");
}



Now write a select query on the table and fill it to a viewbag.In  "Student.cshtml" , a for loop is written which retrieve data from viewbag and create a tabular display of data.


public ActionResult Index()
{
                           LINQtoSQLDataContext db = new LINQtoSQLDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\aa\tutorial.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

                          var stu = (from ord in db.students 
                      select ord).ToList<student>();

                          ViewBag.student = stu;
                         return View("~/Views/Home/Student.cshtml");
}

Output





বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...