public void ShouldHandleSubQueries() { query = new NLinqQuery(@" from m in methods where !m.IsStatic orderby m.Name group m by m.Name into g select new { MethodName = g.Key, Overloads = g.Count() }"); linq = new LinqToMemory(query); linq.AddSource("methods", typeof(string).GetMethods()); IList result = linq.List(); Assert.AreEqual(35, result.Count); query = new NLinqQuery(@" from m in methods where !m.IsStatic orderby m.Name group m by m.Name into g select new { MethodName = g.Key, Overloads = g.Count(), MethodNamesLength = g.Sum( meth => meth.Name.Length ) }"); linq = new LinqToMemory(query); linq.AddSource("methods", typeof(string).GetMethods()); result = linq.List(); Assert.AreEqual(35, result.Count); }
public void ShouldHandleSubQueries2() { query = new NLinqQuery(@" from m in methods where !m.IsStatic orderby m.Name group m by m.Name into g select new { MethodName = g.Key, Overloads = g.Count(), MethodParameters = g.Sum( meth => (from p in meth.GetParameters() select p).Count() ) }" ); linq = new LinqToMemory(query); linq.AddSource("methods", typeof(string).GetMethods()); IList result = linq.List(); Assert.AreEqual(35, result.Count); foreach (Variant v in result) { Console.WriteLine(v["MethodName"] + " " + v["Overloads"] + " " + v["MethodParameters"]); } }
public void ShouldHandleKeywordsInStringLiterals() { string[] words = { " che rry " }; query = new NLinqQuery(@" from m in words select 'true' "); linq = new LinqToMemory(query); linq.AddSource("words", words); Assert.AreEqual("true", linq.List <string>()[0]); }
public void MaxSimple() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Max() "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate <int>(); Assert.AreEqual(9, result); }
public void AverageProjection() { string[] words = { "cherry", "apple", "blueberry" }; query = new NLinqQuery(@" words.Average(w => w.Length) "); linq = new LinqToMemory(query); linq.AddSource("words", words); double result = linq.Evaluate <double>(); Assert.AreEqual(6.66666666666667, result); }
public void MaxProjection() { string[] words = { "cherry", "apple", "blueberry" }; query = new NLinqQuery(@" words.Max(w => w.Length) "); linq = new LinqToMemory(query); linq.AddSource("words", words); int result = linq.Evaluate <int>(); Assert.AreEqual(9, result); }
public void AverageSimple() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Average() "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); double result = linq.Evaluate <double>(); Assert.AreEqual(4.5, result); }
public void CountIndexed() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Count((n, index) => n % 2 == index % 2) "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate <int>(); Assert.AreEqual(4, result); }
public void CountConditional() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Count(n => n % 2 == 1) "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate <int>(); Assert.AreEqual(5, result); }
public void CountSimple() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; query = new NLinqQuery(@" factorsOf300.Distinct().Count() "); linq = new LinqToMemory(query); linq.AddSource("factorsOf300", factorsOf300); int result = linq.Evaluate <int>(); Assert.AreEqual(3, result); }
public void First() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.First((num, index) => (num % 2 == 0) && (index % 2 == 0))" ); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate <int>(); Assert.AreEqual(6, result); }
public void SkipSimple() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" from n in numbers.Skip(4) select n"); List <object> n = new List <object>(); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); IList where1 = linq.List(); Assert.AreEqual(6, where1.Count); }
public void ElementAt() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" (from n in numbers where n < 5 select n ).ElementAt(3)" ); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate <int>(); Assert.AreEqual(2, result); }
public void WhereIndexedBis() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; query = new NLinqQuery(@" from n in digits.Where((digit, index) => digit.Length < index) select n"); List <object> n = new List <object>(); linq = new LinqToMemory(query); linq.AddSource("digits", digits); IList where1 = linq.List(); Assert.AreEqual(5, where1.Count); }
public void WhereIndexedBis() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; query = new NLinqQuery(@" from n in digits.Where((digit, index) => digit.Length < index) select n"); List<object> n = new List<object>(); linq = new LinqToMemory(query); linq.AddSource("digits", digits); IList where1 = linq.List(); Assert.AreEqual(5, where1.Count); }
public void Distinct1() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; query = new NLinqQuery(@" from n in factorsOf300.Distinct() select n"); List <object> n = new List <object>(); linq = new LinqToMemory(query); linq.AddSource("factorsOf300", factorsOf300); IList where1 = linq.List(); Assert.AreEqual(3, where1.Count); }
public void Except() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; query = new NLinqQuery(@" from n in numbersA.Except(numbersB) select n"); linq = new LinqToMemory(query); linq.AddSource("numbersA", numbersA); linq.AddSource("numbersB", numbersB); IList where1 = linq.List(); Assert.AreEqual(5, where1.Count); }
private static object runQueryInternal(string queryText, IDictionary sources) { try { var query = new NLinqQuery(queryText); var linq = new LinqToMemory(query); foreach (string source in sources.Keys) linq.AddSource(source, (IEnumerable)sources[source]); //return linq.Evaluate(); return linq.Enumerate(); } catch (Exception ex) { DI.log.ex(ex, "in runQueryInternal"); return null; } }
public void WhereSimple1() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" from n in numbers where n < 5 select n "); List<object> n = new List<object>(); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); IList where1 = linq.List(); Assert.AreEqual(5, where1.Count); }
public void SelectIndexed() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" from n in numbers.Select((num, index) => new {Num = num, InPlace = (num == index)}) select n"); List <object> n = new List <object>(); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); IList where1 = linq.List(); Assert.AreEqual(10, where1.Count); Assert.AreEqual(false, ((Variant)where1[0])["InPlace"]); Assert.AreEqual(true, ((Variant)where1[3])["InPlace"]); }
private static object runQueryInternal(string queryText, IDictionary sources) { try { var query = new NLinqQuery(queryText); var linq = new LinqToMemory(query); foreach (string source in sources.Keys) { linq.AddSource(source, (IEnumerable)sources[source]); } //return linq.Evaluate(); return(linq.Enumerate()); } catch (Exception ex) { DI.log.ex(ex, "in runQueryInternal"); return(null); } }
public void ShouldHandleNullValues() { List <Person> people = new List <Person>( new Person[] { new Person("B"), new Person("A"), null }); query = new NLinqQuery(@" from p in people where p != null select p "); linq = new LinqToMemory(query); linq.AddSource("people", people); List <Person> result = linq.List <Person>(); Assert.AreEqual(2, result.Count); Assert.AreEqual("B", result[0].Name); Assert.AreEqual("A", result[1].Name); }
public void ShouldSortAList() { List <Person> people = new List <Person>( new Person[] { new Person("B"), new Person("A"), new Person("C") }); query = new NLinqQuery(@" from p in people orderby p.Name select p "); linq = new LinqToMemory(query); linq.AddSource("people", people); List <Person> result = linq.List <Person>(); Assert.AreEqual(3, result.Count); Assert.AreEqual("A", result[0].Name); Assert.AreEqual("B", result[1].Name); Assert.AreEqual("C", result[2].Name); }
public void ShouldProvidePaging() { List <Person> people = new List <Person>( new Person[] { new Person("B"), new Person("A"), new Person("C") }); query = new NLinqQuery(@" (from p in people orderby p.Name select p).Where((p, index) => index > 0) "); linq = new LinqToMemory(query); linq.AddSource("people", people); /// TODO: be able to add linq.AddSource("first", 0); List <Person> result = linq.List <Person>(); Assert.AreEqual(2, result.Count); Assert.AreEqual("B", result[0].Name); Assert.AreEqual("C", result[1].Name); }
public void ShouldHandleEscapeSequence() { string[] words = { " che rry " }; query = new NLinqQuery(@" from m in words select ' \'che rr : y\' ' "); linq = new LinqToMemory(query); linq.AddSource("words", words); Assert.AreEqual(" 'che rr : y' ", linq.List <string>()[0]); query = new NLinqQuery(@" from m in words select 's.ros\u0040evaluant.com' "); linq = new LinqToMemory(query); linq.AddSource("words", words); Assert.AreEqual("*****@*****.**", linq.List <string>()[0]); }
public void MaxProjection() { string[] words = { "cherry", "apple", "blueberry" }; query = new NLinqQuery(@" words.Max(w => w.Length) "); linq = new LinqToMemory(query); linq.AddSource("words", words); int result = linq.Evaluate<int>(); Assert.AreEqual(9, result); }
public void MaxSimple() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Max() "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate<int>(); Assert.AreEqual(9, result); }
public void CountIndexed() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Count((n, index) => n % 2 == index % 2) "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate<int>(); Assert.AreEqual(4, result); }
public void ShouldSortAList() { List<Person> people = new List<Person>( new Person[] { new Person("B"), new Person("A"), new Person("C") }); query = new NLinqQuery(@" from p in people orderby p.Name select p "); linq = new LinqToMemory(query); linq.AddSource("people", people); List<Person> result = linq.List<Person>(); Assert.AreEqual(3, result.Count); Assert.AreEqual("A", result[0].Name); Assert.AreEqual("B", result[1].Name); Assert.AreEqual("C", result[2].Name); }
public void ShouldHandleEscapeSequence() { string[] words = { " che rry " }; query = new NLinqQuery(@" from m in words select ' \'che rr : y\' ' "); linq = new LinqToMemory(query); linq.AddSource("words", words); Assert.AreEqual(" 'che rr : y' ", linq.List<string>()[0]); query = new NLinqQuery(@" from m in words select 's.ros\u0040evaluant.com' "); linq = new LinqToMemory(query); linq.AddSource("words", words); Assert.AreEqual("*****@*****.**", linq.List<string>()[0]); }
public void Distinct1() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; query = new NLinqQuery(@" from n in factorsOf300.Distinct() select n"); List<object> n = new List<object>(); linq = new LinqToMemory(query); linq.AddSource("factorsOf300", factorsOf300); IList where1 = linq.List(); Assert.AreEqual(3, where1.Count); }
public void SelectIndexed() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" from n in numbers.Select((num, index) => new {Num = num, InPlace = (num == index)}) select n"); List<object> n = new List<object>(); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); IList where1 = linq.List(); Assert.AreEqual(10, where1.Count); Assert.AreEqual(false, ((Variant)where1[0])["InPlace"]); Assert.AreEqual(true, ((Variant)where1[3])["InPlace"]); }
public void SkipWhileSimple() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" from n in numbers.SkipWhile(n => n % 3 != 0) select n"); List<object> n = new List<object>(); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); IList where1 = linq.List(); Assert.AreEqual(7, where1.Count); }
static void Main(string[] args) { Person[] people = new Person[] { new Person("Bill", 31), new Person("John", 30), new Person("Cindy", 25), new Person("Sue", 29) }; // For testing physical links people[0].Friends.Add(people[0]); people[0].Friends.Add(people[1]); people[1].Friends.Add(people[2]); people[2].Friends.Add(people[3]); people[3].Friends.Add(people[0]); // For testing logical links Address[] addresses = new Address[] { new Address("Bill", "Redmon"), new Address("Bill", "Boston"), new Address("Cindy", "New York") }; NLinqQuery query; LinqToMemory linq; #region Projections Console.WriteLine(); Console.WriteLine("Projections"); Console.WriteLine(); query = new NLinqQuery( @" from c in people from d in people where c.Age > d.Age select new Evaluant.NLinq.Play.Person ( c.Firstname, d.Age )"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (Person o in linq.Enumerate<Person>()) { Console.WriteLine(o); } #endregion #region Anonymous types Console.WriteLine(); Console.WriteLine("Anonymous types"); Console.WriteLine(); query = new NLinqQuery( @" from c in people select new Evaluant.NLinq.Play.Person { c.Firstname, Age = c.Age * 2}"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (Person o in linq.Enumerate<Person>()) { Console.WriteLine(o); } #endregion #region Physical links Console.WriteLine(); Console.WriteLine("Physical links"); Console.WriteLine(); query = new NLinqQuery( @" from c in people from f in c.Friends select new { c.Firstname, Friend = f.Firstname.Substring(0,1) }"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Ordering Console.WriteLine(); Console.WriteLine("Ordering"); Console.WriteLine(); query = new NLinqQuery( @" from c in people orderby c.Age descending, c.Firstname select c"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (object o in linq.Enumerate<Person>()) { Console.WriteLine(o); } #endregion #region Grouping Console.WriteLine(); Console.WriteLine("Grouping"); Console.WriteLine(); query = new NLinqQuery( @" from m in methods where !m.IsStatic orderby m.Name group m by m.Name into g select new { MethodName = g.Key, Overloads = g.Count() }"); linq = new LinqToMemory(query); linq.AddSource("methods", typeof(string).GetMethods()); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Computed properties Console.WriteLine(); Console.WriteLine("Computed properties"); Console.WriteLine(); query = new NLinqQuery( @" from c in people let t = c.Age * 3 where t % 2 == 0 orderby t descending select new { c.Firstname, t }"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Logical links (Joins) Console.WriteLine(); Console.WriteLine("Logical links (Joins)"); Console.WriteLine(); query = new NLinqQuery( @" from p in people join a in addresses on p.Firstname equals a.Owner select new { p.Firstname, a.City }"); linq = new LinqToMemory(query); linq.AddSource("people", people); linq.AddSource("addresses", addresses); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Useful scenario - Relationship sorting Console.WriteLine(); Console.WriteLine("Useful scenario - Relationship sorting"); Console.WriteLine(); query = new NLinqQuery( @" from f in friends orderby f.Firstname descending select f" ); linq = new LinqToMemory(query); linq.AddSource("friends", people[0].Friends); foreach (Person o in linq.Enumerate<Person>()) { Console.WriteLine(o); } #endregion }
public void AverageSimple() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Average() "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); double result = linq.Evaluate<double>(); Assert.AreEqual(4.5, result); }
public void AverageProjection() { string[] words = { "cherry", "apple", "blueberry" }; query = new NLinqQuery(@" words.Average(w => w.Length) "); linq = new LinqToMemory(query); linq.AddSource("words", words); double result = linq.Evaluate<double>(); Assert.AreEqual(6.66666666666667, result); }
public void First() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.First((num, index) => (num % 2 == 0) && (index % 2 == 0))" ); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate<int>(); Assert.AreEqual(6, result); }
public void ShouldHandleKeywordsInStringLiterals() { string[] words = { " che rry " }; query = new NLinqQuery(@" from m in words select 'true' "); linq = new LinqToMemory(query); linq.AddSource("words", words); Assert.AreEqual("true", linq.List<string>()[0]); }
public void ElementAt() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" (from n in numbers where n < 5 select n ).ElementAt(3)" ); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate<int>(); Assert.AreEqual(2, result); }
public void ShouldProvidePaging() { List<Person> people = new List<Person>( new Person[] { new Person("B"), new Person("A"), new Person("C") }); query = new NLinqQuery(@" (from p in people orderby p.Name select p).Where((p, index) => index > 0) "); linq = new LinqToMemory(query); linq.AddSource("people", people); /// TODO: be able to add linq.AddSource("first", 0); List<Person> result = linq.List<Person>(); Assert.AreEqual(2, result.Count); Assert.AreEqual("B", result[0].Name); Assert.AreEqual("C", result[1].Name); }
public void CountSimple() { int[] factorsOf300 = { 2, 2, 3, 5, 5 }; query = new NLinqQuery(@" factorsOf300.Distinct().Count() "); linq = new LinqToMemory(query); linq.AddSource("factorsOf300", factorsOf300); int result = linq.Evaluate<int>(); Assert.AreEqual(3, result); }
public void ShouldHandleNullValues() { List<Person> people = new List<Person>( new Person[] { new Person("B"), new Person("A"), null }); query = new NLinqQuery(@" from p in people where p != null select p "); linq = new LinqToMemory(query); linq.AddSource("people", people); List<Person> result = linq.List<Person>(); Assert.AreEqual(2, result.Count); Assert.AreEqual("B", result[0].Name); Assert.AreEqual("A", result[1].Name); }
public void CountConditional() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; query = new NLinqQuery(@" numbers.Count(n => n % 2 == 1) "); linq = new LinqToMemory(query); linq.AddSource("numbers", numbers); int result = linq.Evaluate<int>(); Assert.AreEqual(5, result); }
static void Main(string[] args) { Person[] people = new Person[] { new Person("Bill", 31), new Person("John", 30), new Person("Cindy", 25), new Person("Sue", 29) }; // For testing physical links people[0].Friends.Add(people[0]); people[0].Friends.Add(people[1]); people[1].Friends.Add(people[2]); people[2].Friends.Add(people[3]); people[3].Friends.Add(people[0]); // For testing logical links Address[] addresses = new Address[] { new Address("Bill", "Redmon"), new Address("Bill", "Boston"), new Address("Cindy", "New York") }; NLinqQuery query; LinqToMemory linq; #region Projections Console.WriteLine(); Console.WriteLine("Projections"); Console.WriteLine(); query = new NLinqQuery( @" from c in people from d in people where c.Age > d.Age select new Evaluant.NLinq.Play.Person ( c.Firstname, d.Age )"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (Person o in linq.Enumerate <Person>()) { Console.WriteLine(o); } #endregion #region Anonymous types Console.WriteLine(); Console.WriteLine("Anonymous types"); Console.WriteLine(); query = new NLinqQuery( @" from c in people select new Evaluant.NLinq.Play.Person { c.Firstname, Age = c.Age * 2}"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (Person o in linq.Enumerate <Person>()) { Console.WriteLine(o); } #endregion #region Physical links Console.WriteLine(); Console.WriteLine("Physical links"); Console.WriteLine(); query = new NLinqQuery( @" from c in people from f in c.Friends select new { c.Firstname, Friend = f.Firstname.Substring(0,1) }"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Ordering Console.WriteLine(); Console.WriteLine("Ordering"); Console.WriteLine(); query = new NLinqQuery( @" from c in people orderby c.Age descending, c.Firstname select c"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (object o in linq.Enumerate <Person>()) { Console.WriteLine(o); } #endregion #region Grouping Console.WriteLine(); Console.WriteLine("Grouping"); Console.WriteLine(); query = new NLinqQuery( @" from m in methods where !m.IsStatic orderby m.Name group m by m.Name into g select new { MethodName = g.Key, Overloads = g.Count() }"); linq = new LinqToMemory(query); linq.AddSource("methods", typeof(string).GetMethods()); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Computed properties Console.WriteLine(); Console.WriteLine("Computed properties"); Console.WriteLine(); query = new NLinqQuery( @" from c in people let t = c.Age * 3 where t % 2 == 0 orderby t descending select new { c.Firstname, t }"); linq = new LinqToMemory(query); linq.AddSource("people", people); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Logical links (Joins) Console.WriteLine(); Console.WriteLine("Logical links (Joins)"); Console.WriteLine(); query = new NLinqQuery( @" from p in people join a in addresses on p.Firstname equals a.Owner select new { p.Firstname, a.City }"); linq = new LinqToMemory(query); linq.AddSource("people", people); linq.AddSource("addresses", addresses); foreach (object o in linq.Enumerate()) { Console.WriteLine(o); } #endregion #region Useful scenario - Relationship sorting Console.WriteLine(); Console.WriteLine("Useful scenario - Relationship sorting"); Console.WriteLine(); query = new NLinqQuery( @" from f in friends orderby f.Firstname descending select f" ); linq = new LinqToMemory(query); linq.AddSource("friends", people[0].Friends); foreach (Person o in linq.Enumerate <Person>()) { Console.WriteLine(o); } #endregion }