在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):mythz/kotlin-linq-examples开源软件地址(OpenSource Url):https://github.com/mythz/kotlin-linq-examples开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):101 C# LINQ Samples in KotlinThis project contains the C# 101 LINQ Samples rewritten to Kotlin. Like the existing Java LINQ Examples, Kotlin examples run and have their results displayed inside an Android App courtesy of the rich support for Kotlin in Android Studio. Compare Kotlin to other LINQ examples written in: Comparing against Java examples showcases the power and expressiveness of both languages ability for developing native Android Apps where Java 7 is by far the worst language for functional/LINQ-style programming vs Kotlin which is one of the best - resulting in a much more readable and maintainable code-base that's less than 1/2 the size. Kotlin Android ResourcesTo help getting started with Kotlin, we'll maintain links to useful resources helping to develop Android Apps with Kotlin below:
Call .NET Web Services from KotlinIf you're looking to create an effortles typed API for consuming .NET Web Services in JVM or Kotlin Android Apps checkout ServiceStack's Kotlin Add ServiceStack Reference. Running the examplesEach of the LINQ Examples can be run from the included Android App with its results logged to the screen: Run the included Android Studio project to execute all the examples. You can also choose to only run specific examples by commenting out any of the sections you're not interested in MainActivity.kt. A copy of the LINQ examples output is also available in linq-log.txt. ContentsThe samples below mirrors the C# LINQ samples layout with the names of the top-level Java methods matching their corresponding C# examples. LINQ - Restriction Operators / MSDN C#LINQ - Projection Operators / MSDN C#LINQ - Partitioning Operators / MSDN C#LINQ - Ordering Operators / MSDN C#LINQ - Grouping Operators / MSDN C#LINQ - Set Operators / MSDN C#LINQ - Conversion Operators / MSDN C#LINQ - Element Operators / MSDN C#LINQ - Generation Operators / MSDN C#LINQ - Quantifiers / MSDN C#LINQ - Aggregate Operators / MSDN C#LINQ - Miscellaneous Operators / MSDN C#LINQ - Query Execution / MSDN C#LINQ - Join Operators / MSDN C#Kotlin Functional UtilsKotlin has great language and built-in library support to simplify programming in a functional-style where most of the LINQ examples are able to use Kotlins built-in utils. Some of the more advanced LINQ examples like JOIN's were missing in Kotlins standard library, for this we leverage the existing implementations in ServiceStack's Java and Android Client Library: net.servicestack:android. InstallTo include it in your Android Studio project, add it to your build.gradle dependency, e.g:
Pure Java projects should add the net.servicestack:client dependency instead:
Alternatively this library is also automatically added when Adding a Typed Remote Service Reference with ServiceStack IDE Plugins for Intellij IDEA and Eclipse Maven projects. Kotlin ExtensionsOnly a 2 extensions
were required to cover Kotlin's missing Side-by-side - C# LINQ vs KotlinFor a side-by-side comparison, the original C# source code is displayed above the equivalent Kotlin translation.
LINQ - Restriction Operatorslinq1: Where - Simple 1//c#
public void Linq1()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums =
from n in numbers
where n < 5
select n;
Console.WriteLine("Numbers < 5:");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
} //kotlin
fun linq1() {
val numbers = intArrayOf(5, 4, 1, 3, 9, 8, 6, 7, 2, 0)
val lowNums = numbers.filter { it < 5 }
Log.d("Numbers < 5:")
lowNums.forEach { Log.d(it) }
} Output
linq2: Where - Simple 2//c#
public void Linq2()
{
List<Product> products = GetProductList();
var soldOutProducts =
from p in products
where p.UnitsInStock == 0
select p;
Console.WriteLine("Sold out products:");
foreach (var product in soldOutProducts)
{
Console.WriteLine("{0} is sold out!", product.ProductName);
}
} //kotlin
fun linq2() {
val soldOutProducts = products.filter { it.unitsInStock == 0 }
Log.d("Sold out products:")
soldOutProducts.forEach { Log.d("${it.productName} is sold out!") }
} Output
linq3: Where - Simple 3//c#
public void Linq3()
{
List<Product> products = GetProductList();
var expensiveInStockProducts =
from p in products
where p.UnitsInStock > 0 && p.UnitPrice > 3.00M
select p;
Console.WriteLine("In-stock products that cost more than 3.00:");
foreach (var product in expensiveInStockProducts)
{
Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName);
}
} //kotlin
fun linq3() {
val expensiveInStockProducts = products.filter { it.unitsInStock > 0 && it.unitPrice > 3.00 }
Log.d("In-stock products that cost more than 3.00:")
expensiveInStockProducts.forEach { Log.d("${it.productName} is in stock and costs more than 3.00.") }
} Output
linq4: Where - Drilldown//c#
public void Linq4()
{
List<Customer> customers = GetCustomerList();
var waCustomers =
from c in customers
where c.Region == "WA"
select c;
Console.WriteLine("Customers from Washington and their orders:");
foreach (var customer in waCustomers)
{
Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName);
foreach (var order in customer.Orders)
{
Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate);
}
}
} //kotlin
fun linq4() {
val waCustomers = customers.filter { "WA" == it.region }
Log.d("Customers from Washington and their orders:")
waCustomers.forEach { c ->
Log.d("Customer ${c.customerId} ${c.companyName}")
c.orders.forEach { Log.d(" Order ${it.orderId}: ${dateFmt(it.orderDate)}") }
}
} Output
linq5: Where - Indexed//c#
public void Linq5()
{
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var shortDigits = digits.Where((digit, index) => digit.Length < index);
Console.WriteLine("Short digits:");
foreach (var d in shortDigits)
{
Console.WriteLine("The word {0} is shorter than its value.", d);
}
} //kotlin
fun linq5() {
val digits = arrayOf("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
val shortDigits = digits.filterIndexed { i, it -> it.length < i }
Log.d("Short digits:")
shortDigits.forEach { Log.d("The word $it is shorter than its value.") }
} Output
LINQ - Projection Operatorslinq6: Select - Simple 1//c#
public void Linq6()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numsPlusOne =
from n in numbers
select n + 1;
Console.WriteLine("Numbers + 1:");
foreach (var i in numsPlusOne)
{
Console.WriteLine(i);
}
} //kotlin
fun linq06() {
val numbers = intArrayOf(5, 4, 1, 3, 9, 8, 6, 7, 2, 0)
val numsPlusOne = numbers.map { it + 1 }
Log.d("Numbers + 1:")
numsPlusOne.forEach { Log.d(it) }
} Output
linq7: Select - Simple 2//c#
public void Linq7()
{
List<Product> products = GetProductList();
var productNames =
from p in products
select p.ProductName;
Console.WriteLine("Product Names:");
foreach (var productName in productNames)
{
Console.WriteLine(productName);
}
} //kotlin
fun linq07() {
val productNames = products.map { it.productName }
Log.d("Product Names:")
productNames.forEach { Log.d(it) }
} Output
|