working on it ...


Explore Public Snippets

Sort by

Found 645 snippets matching: linq

    public by cghersi  3309  1  8  1

    LINQ: How to retrieve an IEnumerable collection with simple query

    This is a simple query in Linq to retrieve the list of Users that have some specified condition (in this case Active == true). Please note the == operator in the where clause (and in general all C# operators apply). Linq is excellent in writing fast and effective in-memory query on lists, dictionaries, etc.
    ICollection<User> users = new List<User>();
    users.Add(new User("Tom"));
    users.Add(new User("Mark"));
    IEnumerable<User> result = (
    	from user in users 
    	where user.Active == true 
    	select user); 

    public by lbottaro  2965  1  6  0

    Linq Xml tree in c#

    This C# code allows you to create an entire XML tree, using linq and xml nested technique.
    XElement contacts =
        new XElement("Contacts",
            new XElement("Contact",
                new XElement("Name", "Patrick Hines"),                                                 
                new XElement("Phone", "206-555-0144"),
                new XElement("Address",
                    new XElement("Street1", "123 Main St"),
                    new XElement("City", "Mercer Island"),
                    new XElement("State", "WA"),
                    new XElement("Postal", "68042")

    public by lbottaro  2746  0  6  1

    C# linq expression to check all data in a given List

    This c# linq lambda function example shows how to verify if all items in a List are present in another List object. You don't need to loop or iterate for each element, just use the All extension method.
    List<string> myList = new List<string>();
    // myTargetList is a given List<string>
    Assert.IsTrue(myList.All(x => myTargetList.Contains(x)), "myList has element missing in myTargetList!"));

    public by lbottaro  1847  0  5  1

    C# LINQ query lambda expression

    This c# code is a LINQ query to easily check data value into a List without looping it
    List<MyObj> actual = new List<MyObj>();
    Assert.IsTrue(actual.Exists(x => x.UserID == userKvp.Value.ID), string.Format("UserID missing! Expected: {0}, GroupID: {1}", userKvp.Value.ID, groupID));

    public by lbottaro  1872  0  6  0

    Linq list search and occurrence counter

    This c# code allows to search through a list for a specific pattern and count the number of matches found.
    int numOfFound= -1;
    string myTarget = "test";
    numOfFound = myList.Count(x => x.Equals(myTarget));
    Assert.IsTrue(numOfFound == 1, string.Format("Error! {0}", numOfFound));

    public by snip2code  3040  1  5  0

    Filtered COUNT (*) in Linq

    This is a very effective method to count in-memory a given collection (Users), with an established filter (active == true). Really fast, really simple, Linq style!!
    (from user in Users where user.Active == true select user).Count();

    public by jimonearth  2924  0  4  1

    C# LINQ 'ascending' and 'descending' Example

    // C# LINQ 'ascending' and 'descending' Example
    // Because ascending is the default sort order, you don't actually need to use it when you want to sort. 
    // You can just omit this keyword and the query expression will function the same way. 
    // It is in a sense a form of syntactic sugar.
    using System;
    using System.Linq;
    public class Employee
        public int Salary { get; set; }
        public int Id { get; set; }
    public class Program
        public static void Main()
    	Employee[] array = new Employee[]
    		new Employee(){Salary = 40000, Id = 4},
    	    new Employee(){Salary = 40000, Id = 0},
    	    new Employee(){Salary = 60000, Id = 7},
    	    new Employee(){Salary = 60000, Id = 9}
    	// Highest salaries first.
    	// Lowest IDs first.
    	// The first property specified in the orderby clause is the primary sort.
    	var result = from em in array
    				orderby em.Salary descending, em.Id ascending
    		     	// orderby em.Id ascending, em.Salary descending
    		     	select em;
    	foreach (var em in result)
    	    Console.WriteLine("{0}, {1}", em.Salary, em.Id);

    public by SpeedOfSpin  2433  2  3  0

    Grouping data using Linq

    var data = File.ReadAllText("c:\\temp\\ftshorse.csv").FromCsv<List<Horse>>();
    var grouped = data.GroupBy(a => new {a.event_date, a.meeting, a.event_time}).Select(a=> new Race()
        Meeting = a.Key.meeting, EventDate = a.Key.event_date, EventTime = a.Key.event_time, Horses = a.ToList()

    public by chrisguiblin  1986  0  6  0

    Linq - Get sequence value of result

    Sequence value will be the rank order in which the result lives
    var sequence = allResults.AsEnumerable().Select((s, seq)
                  => new
                      Sequence = seq,
                      Franchise_Code = s.Max(x => x.ID),
                      Duration = s.Average(x => x.someValue) //Get average duration for each franchise code

    public by chrisguiblin  1541  0  6  0

    Linq - Get average of a joined group of values

    var allResults = from d in myData
                     group d by d.ID into g //Group all IDs together so we only return one result for each
                     join f in moreData on g.Max(x=>x.ID) equals f.ID into j //Put the results of f and g into a new value j
                     orderby j.Average(x=>x.someValue) //Get the average value of a field of data
                     select j;
    • Public Snippets
    • Channels Snippets