Linq how parameters are passed in select - linq

I am learning LINQ and I found this example.
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace)
{
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}
I don't understand execution of the below line:
var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
As per my understanding num and index are parameters, but I don't understand where we decide that first parameter will be a number and second parameter will be index of the number?
Is it something that is fixed for int type of arrays?
Can anybody please help me understanding this?
Thanks in advance.

The definition of method Select decides it.
There are at lest 2 definitions for Select and one says it takes Func<MyInput,int,MyReturn>. We know that all but the last generic parameter of Func are inputs, and the last one is output.
Func is a special type of object (type of delegate) that has a method called Invoke() to run it and a special syntax-sugar - shorthand () which says that you can omit the Invoke and just write ().
Func<object, int> d = x => 1
d.Invoke(null) // will always return 1
d(new Object()) // also the same
So compiler takes your function and tries to find one of Selects that can accept this kind of function. Then it compiles. And then in run-time the Select just takes first, second etc. element from collection and runs your function by passing the element (and the index if the overload of Select with the function that accepts index has been chosen).

(num, index, TResult) => new { .... } is an anonymous function of type Func delegate.
It is defined by .Net Framework. Thus the First parameter is an object, where as second parameter is index of an object in an array.
Func<T, Index, TResult> Delegate
Func translated to English is: “A method that takes an T and Index of T in an array, and returns a TResult.
There is another thing called Action delegate, where there is no return.
Please have a reference to this link:
http://simpleprogrammer.com/2010/09/24/explaining-what-action-and-func-are/

See this link.
The first argument to selector represents the element to process. The second argument to selector represents the zero-based index of that element in the source sequence. This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. It can also be useful if you want to retrieve the index of one or more elements.

Related

Write a function index -- takes list and a number, and returns the index of the first element with the specified value

Wouldn't this question need 3 parameters instead of 2, being the list, index, and item being added?
You don't tell us what language you're working in but your function will have 2 parameters: the list and the value looked for, and 1 return type. If it is Java it might be:
public static int findIndex(String[] myList, String valueToLookFor) {
int indice;
// calculations here
return indice;
}
I believe in some languages you can speak if IN parameters and OUT parameters in which case yes, there are 2 IN parameters plus 1 OUT parameter = 3.

Linq statement involving index based update of value not working

In my C# winforms project, I wanted to update a specific index based position in a collection (named List l1 here).
I tried below code:
l1.Where((s, i1) => i1 == intvalue).Select(s => { if (s > 0) s = -1; return s; };
I wanted to set value at invalue index to -1 in the list l1, but when I do so with above statement the value in l1 is not changed. Please help! I am new to Linq and have searched the topic 'index based change of value in a collection' everywhere, but can't resolve my problem as it involves BigInteger type and I have so many elements in the list that their total count passes the allowed max value for int type in c#. So when I type l1[intvalue] it says can't convert BigInteger to int for index position.
Thanks
LINQ is Language INtegrated Queries. It's purpose is querying data. If you want to modify list item:
if (l1[intvalue] > 0)
l1[intvalue] = -1;
Also I'd like to explain why your query is not changing list.
On first step you are selecting list items by some condition. Very strange condition by the way. If you want to select item by index, there is operator ElementAt.
Then you are doing projection. I.e. you are calling anonymous method which accepts each selected item and produce some result. Each item passed to that method as s argument. And when you are assigning -1 to s you are actually assigning value to method argument. That does not affect items in the list. Even if your list will contain items of reference type instead of integers, assigning value to method argument will just change where argument variable points. It will not change references in original list. Though you still can modify items of reference type. But such side-effects in projection methods are not good practice.

Linq mixed with string.Compare(...,...,...)

I was searching for a solution to permform a linq query and ignore case. I found this:
m_context.Users.SingleOrDefault(u => string.Compare(u.UserName, username, StringComparison.InvariantCultureIgnoreCase) == 0);
It search for a user object based on the username provided, ignoring case. It works, that's not the question here, but when analysing the code it seems strange to me. I mean, inside the linq, we have the string.Compare(...,...,...) returning an integer. So what? How is it managed by linq (SingleOrDefault)?
Thanks for your help.
You are passing a predicate into the SingleOrDefault method. The predicate evaluates to true or false, and this method returns the single element in the sequence that satisfies that predicate.
u => string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase) == 0
This is a Func<User, bool> predicate, which means it is a function that accepts a User as an argument u and returns a boolean value as a result of the string.Compare(...) == 0 evaluation. The single element in the sequence of users to satisfy this condition is then returned. If more than one satisfies the predicate, it is an error. If less than one satisfies the predicate, you get the default value for the type, which for a reference type is simply null.
Think of it as very roughly
public static T SingleOrDefault<T>(this IEnumerable<T> sequence, Func<T, bool> predicate)
{
T foundItem = null;
int count = 0;
foreach (T item in sequence)
{
if (predicate(item)) // evaluates the u => string.Compare(...)
{
count += 1;
if (count > 1)
throw new InvalidOperationException("...");
foundItem = item;
}
}
return foundItem;
}
The above is again just my rough draft of what the method does, not the actual implementation. If you're interested in a more in-depth investigation of linq-to-objects implementations, consider reading Jon Skeet's Edulinq series, where he goes through and reimplements every (give or take) method and explains it along the way. Again, that's not the actual source code of the library, but it is very educational.
You have string.Compare(...) == 0 - that's Boolean

Explain the below Linq Query?

results.Where(x=>x.Members.Any(y=>members.Contains(y.Name.ToLower())
I happened to see this query in internet. Can anyone explain this query please.
suggest me a good LINQ tutorial for this newbie.
thank you all.
Edited:
what is this x and y stands for?
x is a single result, of the type of the elements in the results sequence.
y is a single member, of the type of the elements in the x.Members sequence.
These are lambda expressions (x => x.whatever) that were introduced into the language with C# 3, where x is the input, and the right side (x.whatever) is the output (in this particular usage scenario).
An easier example
var list = new List<int> { 1, 2, 3 };
var oddNumbers = list.Where(i => i % 2 != 0);
Here, i is a single int item that is an input into the expression. i % 2 != 0 is a boolean expression evaluating whether the input is even or odd. The entire expression (i => i % 2 != 0) is a predicate, a Func<int, bool>, where the input is an integer and the output is a boolean. Follow? As you iterate over the query oddNumbers, each element in the list sequence is evaluated against the predicate. Those that pass then become part of your output.
foreach (var item in oddNumbers)
Console.WriteLine(item);
// writes 1, 3
Its a lambda expression. Here is a great LINQ tutorial
Interesting query, but I don't like it.
I'll answer your second question first. x and y are parameters to the lambda methods that are defined in the calls to Where() and Any(). You could easy change the names to be more meaningful:
results.Where(result =>
result.Members.Any(member => members.Contains(member.Name.ToLower());
And to answer your first question, this query will return each item in results where the Members collection has at least one item that is also contained in the Members collection as a lower case string.
The logic there doesn't make a whole lot of sense to me with knowing what the Members collection is or what it holds.
x will be every instance of the results collection. The query uses lambda syntax, so x=>x.somemember means "invoke somemember on each x passed in. Where is an extension method for IEnumerables that expects a function that will take an argument and return a boolean. Lambda syntax creates delegates under the covers, but is far more expressive for carrying out certain types of operation (and saves a lot of typing).
Without knowing the type of objects held in the results collection (results will be something that implements IEnumerable), it is hard to know exactly what the code above will do. But an educated guess is that it will check all the members of all the x's in the above collection, and return you an IEnumerable of only those that have members with all lower-case names.

LINQ and CASE Sensitivity

I have this LINQ Query:
TempRecordList = new ArrayList(TempRecordList.Cast<string>().OrderBy(s => s.Substring(9, 30)).ToArray());
It works great and performs sorting in a way that's accurate but a little different from what I want. Among the the result of the query I see something like this:
Palm-Bouter, Peter
Palmer-Johnson, Sean
Whereas what I really need is to have names sorted like this:
Palmer-Johnson, Sean
Palm-Bouter, Peter
Basically I want the '-' character to be treated as being lower than the character so that names that contain it show up later in an ascending search.
Here is another example. I get:
Dias, Reginald
DiBlackley, Anton
Instead of:
DiBlackley, Anton
Dias, Reginald
As you can see, again, the order is switched due to how the uppercase letter 'B' is treated.
So my question is, what do I need to change in my LINQ query to make it return results in the order I specified. Any feedback would be greatly appreaciated.
By the way, I tried using s.Substring(9, 30).ToLower() but that didn't help.
Thank you!
To customize the sorting order you will need to create a comparer class that implements IComparer<string> interface. The OrderBy() method takes comparer as second parameter.
internal sealed class NameComparer : IComparer<string> {
private static readonly NameComparer DefaultInstance = new NameComparer();
static NameComparer() { }
private NameComparer() { }
public static NameComparer Default {
get { return DefaultInstance; }
}
public int Compare(string x, string y) {
int length = Math.Min(x.Length, y.Length);
for (int i = 0; i < length; ++i) {
if (x[i] == y[i]) continue;
if (x[i] == '-') return 1;
if (y[i] == '-') return -1;
return x[i].CompareTo(y[i]);
}
return x.Length - y.Length;
}
}
This works at least with the following test cases:
var names = new[] {
"Palmer-Johnson, Sean",
"Palm-Bouter, Peter",
"Dias, Reginald",
"DiBlackley, Anton",
};
var sorted = names.OrderBy(name => name, NameComparer.Default).ToList();
// sorted:
// [0]: "DiBlackley, Anton"
// [1]: "Dias, Reginald"
// [2]: "Palmer-Johnson, Sean"
// [3]: "Palm-Bouter, Peter"
As already mentioned, the OrderBy() method takes a comparer as a second parameter.
For strings, you don't necessarily have to implement an IComparer<string>. You might be fine with System.StringComparer.CurrentCulture (or one of the others in System.StringComparer).
In your exact case, however, there is no built-in comparer which will handle also the - after letter sort order.
OrderBy() returns results in ascending order.
e comes before h, thus the first result (remember you're comparing on a substring that starts with the character in the 9th position...not the beginning of the string) and i comes before y, thus the second. Case sensitivity has nothing to do with it.
If you want results in descending order, you should use OrderByDescending():
TempRecordList.Cast<string>
.OrderByDescending(s => s.Substring(9, 30)).ToArray());
You might want to just implement a custom IComparer object that will give a custom priority to special, upper-case and lower-case characters.
http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx

Resources