Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Saturday, 22 March 2014

linq query like operator

Like operator is not working properly in LINQ instead of it we can use the method Contains.

Using the Like Operator in Linq Query to get the result set, We use Contains.

     string Name = "test";
        var prsn = from o in objValue
                      orderby   o.WorkingDate descending 
                      where o.Name.Contains(Name) //no data is rendered when you have two or more text query unlike one text alone.
                      select o ;

linq join two lists

Join is self defined at the joining of two or more data together; Here I am instructed join of list object in C#;

@linq join two lists
var result = from Item1 in listItem1
             join Item2 in listItem2
             on Item1.Id equals Item2.Id // Join values
             select new { Item1, Item2 };

@get the result in form of concrete list,  needed to call .ToList()
Get the result in the form of list
  var list3 = (from Item1 in list1
             join Item2 in list2
             on Item1.Id equals Item2.Id // join on some property
             select new { Item1, Item2 }).ToList()

@Linq Union operator in C#
Linq Union operator will give the combined unique result of to list contains,
static void Main(string[] args)
        {
            List<string> name1 = new List< string >() { "a", "b", "c"};
            List< string > name2 = new List< string >() {"c", "d" };
            List<string> uniqueName = name1.Union(name2).ToList();
            foreach(string st in uniqueName)
            {
                Console.WriteLine(st);
            }
        }
It will give the result as
a
b
c
d

read xml by linq in c#

Reading data from Linq in c#,  It is very simple and less line of code to read data from XML. It consist of XDocument to load the xml data
Need to import the 
using System.Xml.Linq;
@XML Sample
xml version="1.0" encoding="utf-8" ?>
<root>
  <Employee name="Bob" Phonenumber="344343433" >
   
  </Employee>
  <Employee name="julie" Phonenumber="1212121212" >
  </Employee>
 
</root>
@Code Snippet To read XML in C# using Linq;
class Program
    {
        ///
        /// Main method
        ///

        ///
        public static void Main(String[] args)
        {
            //Load xml
            XDocument xdoc = XDocument.Load(@"E://Employee.xml");
            //Run query
            var lv1s = from lv1 in xdoc.Descendants("Employee")
                       select new
                       {
                           name = lv1.Attribute("name").Value,
                           Phonenumber = lv1.Attribute("Phonenumber").Value
                           //  Children = lv1.Descendants("Detail") // it used to read the desedent
                       };
            //Loop through results
            foreach (var lv1 in lv1s)
            {
                Console.WriteLine("Name : " + lv1.name + " -- Phone Number : " + lv1.Phonenumber);
             /*   foreach (var lv2 in lv1.Children)
                    result.AppendLine("     " + lv2.Attribute("name").Value); * // Reading children values*/
            }
        }
    }
@OutPut

Thursday, 20 March 2014

what is the difference between using exists over contains

1.       Exists is not the linq standard; Exist is there from 2.0;
2.       Just as IndexOf, Contains search strings. It checks if one of the substring is contained in another.
3.       Exists returns whether any of the List elements found. You can use a loop and if-statement also;
4.       The difference is only  Contains support by Linq parametrs to Entities, Exists does not supported
5.       How to use Exists in c#

class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int> { 1, 2, 3, 4 };
            // See if any elements equal to 3
            bool exists = list.Exists(element => element == 3); // True
            Console.WriteLine(exists);
            // Check for numbers eqal to 5
            exists = list.Exists(element => element == 5); // False
            Console.WriteLine(exists);
        }
    }

6.       How to Use Contain in c#
class Program
    {
        static void Main(string[] args)
        {
            string s1 = "Check for the contains method in C#";
            string s2 = "contains"; // Contains is case sensetive
            bool bln;
            bln = s1.Contains(s2);
            Console.WriteLine("string, s2, in string, s1?: {0}", bln); //bln =true
        }
    }