This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What I need is to order IQueriable (the result of LINQ to SQL method) and return first count elements if count is defined. I use the following code:
IOrderedQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0) list = list.Take(count).OrderByDescending(item=>item.Time);
It works fine, but I don't like calling OrderByDescending twice. Can I make it any better without breaking the order and also without double-ordering?
There is absolutely no need for the second OrderByDescending. Simply remove it:
IQueryable<MainLog> list = logs.OrderByDescending(item => item.Time);
if (count > 0)
list = list.Take(count);
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've been while out of C# and MVC. And I am really struggling with the following error, I really don't see it. I have a list of restrictions and i want to add their keys to an string[].
int cntr = 0;
//loop through restrictions and add to array
foreach (var Restriction in this.admingroupRepository.Context.AdminRestrictions.ToList())
{
currentRestrictionKeys[cntr] = Restriction.Key;
cntr += 1;
}
This is the error i get on the cntr += 1 line:
Index was outside the bounds of the array.
I don't understand where this comes from, the foreach breaks before the cntr is out of the array's bounds right?
You have allocated too little space for currentRestrictionKeys. But you don't really need to preallocate it at all; you can just use a trivial projection with LINQ:
var currentRestrictionKeys = this.admingroupRepository.Context.AdminRestrictions
.Select(r => r.Key)
.ToArray();
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
To cut to the chase, when performing some calculations within my code i have a result that is something like. 7.6742332E-30, i have this value stored in a double variable for example, double result = 7.6742332E-30;
When i later check whether this value is greater than 0 the result is true, that it is greater than 0, i assume due to the 7.6742332.
So my question is this, why is the E-30 not being considered and how do i resolve this?
Any advice would be great and thanks so much i advance!
7.6742332E-30 is 0.0000000000000000000000000000076742332, which is a positive number.
7.6742332E-30 mathematcally equals 7.6742332 x 10^-30 which is a positive number
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am working on a linq method and cannot seem to get the return type to match the method signature. Any guidance would be much appreciated.
Thanks!
private static IEnumerable<KeyValuePair<string, string>> RunQuery(XDocument doc)
{
var data = from b in doc.Descendants("Company")
where b.Attribute("name").Value == "CompanyA"
from y in b.Descendants("Shirt")
from z in y.Descendants("Size")
select new
{
color = y.Attribute("id").Value,
price = z.Value
};
return data;
}
You have to create KeyValuePairs.
...
select new KeyValuePair(y.Attribute("id").Value, z.Value)
You can make your query much sorter. Note I've removed from y in b.Descendants("Shirt"), because Descendants parse the whole xml node including all it's descendants up to lowest level. This query will return Dictionary<string, string>, which implements IEnumerable<KeyValuePair<string, string>>, so you don't need to change method signature, but I strongly recommend to do this, because client's won't be able to access dictionary elements with constant time
return doc.Descendants("Company")
.Where(node => node.Attribute("name").Value == "CompanyA")
.Descendants("Size")
.ToDictionary(node => node.Attribute("id").Value,
node => node.Value);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I need to put a comma before the last char of a string.
For example:
Input: 101919 = Ouput: 10191,9
What is the best way to do that?
if (input.Length > 0) { input = input.Insert(input.Length - 1, ","); }
With the insert method
strTarget = strTarget.Insert( srtrTarget.Length -1, ",");
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm porting a c++ class to C# and i have a difficulty.
I would like to find an equivalent of SpanIncluding.
Here is my cpp code :
while (Notes.Mid(j,1).SpanIncluding("0123456789").IsEmpty()!=NULL){}
Anyone can help me please ?
I believe SpanIncluding starts matching from the start of the string, stopping when the first non-matching character is found.
So one formulation in the general case would be this:
string match = new string(someString.ToCharArray().
TakeWhile(c => "0123456789".Contains(c)).ToArray());
(or an equivalent using a regular expression).
However, in the example given in the question there's only one character so the whole thing probably boils down to a test of whether this character is >= '0' and <= '9':
while(char.IsDigit(Notes[j])) { ... };
I found the MSDN page for SpanIncluding, and it seems like a ridiculously specific function. I can't really understand what it tries to solve, since it has some strange caveats.
LINQ would be one way of implementing it:
string text = "2334562";
IEnumerable<char> spannedChars = text.TakeWhile(c => "1234567890".Contains(c));
This is a more direct port of SpanIncluding than queen3's option, if I understand the MSDN page correctly, because the result set should stop the minute it hits a character not in the spanning string.