score:0
var smiths = new list<persons>();
// get all smiths and print them
foreach(var item in newlist)
{
if(item.last == "smith")
smiths.add(item);
}
foreach(var item in smiths)
{
console.writeline(item);
}
score:2
newlist.sort(new lastnamecomparer());
class lastnamecomparer: icomparer<employee>
{
public int compare(employee e1, employee e2)
{
// your comparison logic here that compares two employees
return string.compare(e1.last, e2.last);
}
}
score:2
list<employee> newlist = new list<employee>
{
new employee {first = john, last = smith, age = 30},
new employee {first = jane, last = smith, age = 28},
new employee {first = greg, last = lane, age = 24},
}
// original code: // pre-linq translation:
var last // becomes: var last = new list<employee>();
from name in newlist // becomes: foreach (var name in newlist) {
where name.last.equals("smith") // becomes: if (name.last.equals("smith") {
select name // becomes: last.add(name) } }
// pre-linq code:
var last = new list<employee>();
foreach (var name in newlist)
{
if (name.last.equals("smith")
{
last.add(name)
}
}
Source: stackoverflow.com
Related Query
- Comparing code prior to LINQ
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- How are people unit testing code that uses Linq to SQL
- Comparing Arrays using LINQ in C#
- Syntax to execute code block inside Linq query?
- LINQ Comparing Two Lists - Add new, remove old, leave the ones in common
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Linq to SQL .Equals returns true when comparing int to string --> Id.Equals("5") returns true
- Suggestions for designing complex LINQ code
- Left outer join using LINQ -- understanding the code
- How to pass LinQ Expressions from F# to C# code
- Avoiding code repetition when using LINQ
- Linq to SQL: Where clause comparing a Nullable<DateTime> with a SQL datetime null column
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Comparing GUID to string in LINQ to Entites throws error
- How can I switch that code to use LINQ
- How does this linq code that splits a sequence work?
- multiple orderby in this linq code
- Comparing dates in linq
- Comparing two collections with IEquatable while using only LINQ
- How can I combine this code into one or two LINQ queries?
- Linq with where clause in many-to-many EF Code First object
- LINQ for comparing two lists with complex entities
- Why do these three pieces of LINQ code produce different (or erroneous) results?
- Refactor Linq code and "LINQ to Entities does not recognize the method"
More Query from same tag
- Retrieving latest associated record in LINQ?
- Method to return records true false or all
- How to Check Whether an Array (or List) Contains Another an Array (List) Using LINQ
- C# Lambda Method syntax to obtain attribute values that match pattern in LINQ to XML
- Convert LINQ expressions between model and DTO for use in repositories and DAO
- Dynamic Linq query using
- VB.NET and LINQ. What am I missing here?
- Compute running total in c# from linq query
- Indexed search using linq
- Dynamic filtering using Linq
- Dynamic Filters with Entity Framework 3.5 and Eager Loading
- Calculate mins in Linq
- C# List Comprehensions = Pure Syntactic Sugar?
- Intersection of 6 List<int> objects
- How to get row count 3 table join and lambda expressions?
- linq case insensitive (without toUpper or toLower)
- Linq select - handle situation where out of bounds exception would be thrown
- LINQ query for finding one item in list AND verifying list does not contain another item
- Paging with GridView and LINQ to SQL
- Linq GroupBy Clause not including items with zero count
- LINQ - Previous Record
- Expression... LINQ to Entities only supports casting Entity Data Model primitive types”
- ASP.net MVC3 entities, don't know how to count
- Get present primary keys from link table, set Checkstate checklistbox
- C#: ToArray performance
- Entity Framework - AddOrUpdate using Linq and Lambda
- Perform a search with LINQ and ignoring characters like î or ô or ê
- This row has been removed from a table and does not have any data
- Blow dictionary out to a list
- ASP GridView DataBind with Entity Navigated property