// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty { public struct Maybe { public T Value; public bool HasValue; } public static class MaybeExtensions { public static Maybe Select(this Maybe a, Func fn) { #if CS71 return default; #else return default(Maybe); #endif } public static Maybe Where(this Maybe a, Func predicate) { #if CS71 return default; #else return default(Maybe); #endif } } public class QueryExpressions { public class MaybeHolder { public Maybe Value; #if CS71 public Maybe this[int index] => default; #elif CS60 public Maybe this[int index] => default(Maybe); #endif public Func> Factory() { #if CS71 return () => default; #else return () => default(Maybe); #endif } } public class HbmParam { public string Name { get; set; } public string[] Text { get; set; } } public class Customer { public int CustomerID; public IEnumerable Orders; public string Name; public string Country; public string City; } public class Order { public int OrderID; public DateTime OrderDate; public Customer Customer; public int CustomerID; public decimal Total; public IEnumerable Details; } public class OrderDetail { public decimal UnitPrice; public int Quantity; } public IEnumerable customers; public IEnumerable orders; public object MultipleWhere() { return from c in customers where c.Orders.Count() > 10 where c.Country == "DE" select c; } public object SelectManyFollowedBySelect() { return from c in customers from o in c.Orders select new { c.Name, o.OrderID, o.Total }; } public object SelectManyFollowedByOrderBy() { return from c in customers from o in c.Orders orderby o.Total descending select new { c.Name, o.OrderID, o.Total }; } public object MultipleSelectManyFollowedBySelect() { return from c in customers from o in c.Orders from d in o.Details select new { c.Name, o.OrderID, d.Quantity }; } public object MultipleSelectManyFollowedByLet() { return from c in customers from o in c.Orders from d in o.Details let x = (decimal)d.Quantity * d.UnitPrice select new { c.Name, o.OrderID, x }; } public object FromLetWhereSelect() { return from o in orders let t = o.Details.Sum((OrderDetail d) => d.UnitPrice * (decimal)d.Quantity) where t >= 1000m select new { OrderID = o.OrderID, Total = t }; } public object MultipleLet() { return from a in customers let b = a.Country let c = a.Name select b + c; } public object HibernateApplyGeneratorQuery() { return (from pi in customers.GetType().GetProperties() let pname = pi.Name let pvalue = pi.GetValue(customers, null) select new HbmParam { Name = pname, Text = new string[1] { (pvalue == null) ? "null" : pvalue.ToString() } }).ToArray(); } public object Join() { return from c in customers join o in orders on c.CustomerID equals o.CustomerID select new { c.Name, o.OrderDate, o.Total }; } public object JoinInto() { return from c in customers join o in orders on c.CustomerID equals o.CustomerID into co let n = co.Count() where n >= 10 select new { Name = c.Name, OrderCount = n }; } public object OrderBy() { return from o in orders orderby o.Customer.Name, o.Total descending select o; } public object GroupBy() { return from c in customers group c.Name by c.Country; } public object ExplicitType() { return from Customer c in customers where c.City == "London" select c; } public object QueryContinuation() { return from c in customers group c by c.Country into g select new { Country = g.Key, CustCount = g.Count() }; } public object Issue437(bool[] bools) { return from x in bools where x select (x); } public object SelectIntoContinuation() { return from c in customers select c.Name into n where n.Length > 3 select n.ToUpper(); } #if CS60 private List Issue2545(List arglist) { return arglist?.OrderByDescending((string f) => f.Length).ThenBy((string f) => f.ToLower()).ToList(); } public Maybe? NullConditionalValueTypeQuery(MaybeHolder holder) { return holder?.Value.Where((int value) => value > 0).Select((int value) => value.ToString()); } public Maybe? NullConditionalNestedInvocationQuery(MaybeHolder holder) { return holder?.Factory()().Where((int value) => value > 0).Select((int value) => value.ToString()); } public Maybe? NullConditionalIndexerQuery(MaybeHolder holder) { return holder?[0].Where((int value) => value > 0).Select((int value) => value.ToString()); } #endif public static IEnumerable Issue1310a(bool test) { IEnumerable enumerable = (test ? (from c in Enumerable.Range(0, 255) where char.IsLetter((char)c) select (char)c) : (from c in Enumerable.Range(0, 255) where char.IsDigit((char)c) select (char)c)); return enumerable.Concat(enumerable); } public static Maybe Cast(Maybe a) where TB : class { return from m in a let t = m as TB where t != null select t; } #if CS70 public IEnumerable Issue3352() { // We don't have backward type inference, so LINQ syntax cannot be used with named tuple types. return new (string, int)[4] { ("A", 10), ("B", 20), ("C", 30), ("D", 40) }.Where(((string Name, int Age) t) => t.Age >= 18).Select(((string Name, int Age) t) => t.Name); } #endif } }