LinQ Queires

List of LinQ queries

  1. Aggregate:

  2. 1
    2
     var errors = Error.Aggregate("Error occured: ",
                                (current, error) => current + $"Code - {error.Code}, Detail - {error.Text}; ");
    
  3. Select Many:

  4. IEnumerable data;
                if (data.SelectMany(s => s.Category)
                                     .Where(si => si.SubCategory.Any(si => si.Selected)).Select(ti => ti.SubCategoryId)
                                     .Distinct().Count() > 1)
                {
                    //
                }
  5. For Each:

  6. Parallel.ForEach(Categories,
                  Category =>
                  {
                      Logger.Log(
                         Category.Id);
                  };
    
                parameters.ToList().ForEach(x => command.Parameters.Add(x));
    
    foreach (var node in xnode.Descendants("node1"))
     {
                    node.Nodes().ToList().ForEach(n => n.Remove());
     }
  7. Order By:

  8. data.OrderBy(s => s.date).ToArray();
  9. Join:

  10. $"{Error} (Description: {string.Join(", ", ErrorCodes?.Select(e => e.ToString()))}";
    
    var amount = from a in price.LineItems
                 join b in discount.LineItems on a.Id equals b.Id
                 where (a.amount ?? 0)) > b.amount)
                 select new { a, b };
  11. Select New:

  12.  return (from cat in result.Cast<dynamic>()
                        group cat by cat.Id
                               into bycat
                        let subcat =
                            from region in bycat
                            group region by region.Region
                                into byRegion
                            let subsubcat =
                                        from code in byRegion
                                    group code by code.Code
                                            into byCode
                                    select (string)byCode.Key
                            select new
                            {
                                Name = (string)byRegion.Key,
                                Codes =
                                            subsubcat.ToList()
                            }
                        select new Cat<string>
                        {
                            Name = byCat.Key,
                            Regions =
                                subcat.ToDictionary(r => r.Name, r => r.Codes,
                                    StringComparer.InvariantCultureIgnoreCase)
    
                        }).ToDictionary(c => c.Name, c => c, StringComparer.InvariantCultureIgnoreCase);
            }
    
            select new Info((new[]
                    {
                        cat.Select(lineItem => lineItem.LineItem).ToList()
                    })
                    .ToList()
                    .ForEach(Infos.Add);
  13. Group By:

  14. foreach (var grpCat in items.GroupBy(cat => cat.Id))
                {
                }
  15. TakeWhile

    1.  foreach (var cat in categories.TakeWhile(esi => categories == null))
        {
        }
  16. IEnumerable:

  17.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    using System.Collections.Generic;
    
    namespace Web.Models
    {
        public static class Class
        {
            IEnumerable<CatInfo> data;
            if (TryExecuteQuery(out data, "GetData", parameters))
            {
                info = data.ToList();
            }
        }
    }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...