It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I need a discussion regarding the Performance of LINQ and Lambda Expression.
Which one is better?
I guess you mean query expression when talking about LINQ here.
They are equivalent. The compiler changes the query expression into the equivalent Lambda expression before compiling it, so the generated IL is exactly the same.
Example
var result = select s from intarray
where s < 5
select s + 1;
is exactly the same as
var result = intarray.Where( s => s < 5).Select( s => s+1);
Note that if you write the query expression like this:
var result = select s from intarray
where s < 5
select s;
It's converted to:
var result = intarray.Where( s => s < 5);
The final call to Select is omitted because it's redundant.
a quick comparison in reflector would probably do the trick. However, from a 'preference' standpoint, I find lambda statements easier to follow and write and use them across the board whether it be with objects, xml or whatever.
If performance is negligible, i'd go with the one that works best for you.
i actually started off a little topic looking at linq methods which may be of interest:
What's your favourite linq method or 'trick'
cheers..
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Say I have strings like these
Sam is Boy
Joseph is Boy
Jasmine is Girl
Annie is Girl
Chris is Boy
I have a quick and murky way of preparing a C# Dictionary like this..!
input.ForEach(i =>
{
string[] values = i.Split();
input_dictionary.Add(values[0], values[2]);
});
Do we have any other better/optimised way of achieving this, since the input data follows a fixed format like "Name is Gender"?
Here's a regex pattern you could use:
(.+) is (Boy|Girl)
The following is probably faster, but you should test it.
input_dictionary = (from i in input
let n = i.IndexOf(' is ')
select new { Name = i.Substring(0, n), Sex = i.Substring(n + 4) }
).ToDictionary(i => i.Name, i => i.Sex);
You can also the Regex class, which might be faster or slower than the above. It's difficult to say without testing.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
When I use the code below, it compiles but the rest of the code doesn't seem to work. When I take out the Substring part of it, it does.
-Steps
String theDate, theWeekDay;
if (ToTime(Time[0]) == ToTime(0, 0, 0))
{
theDate=ToDay(Time[0]).ToString().Substring(0,3);
theWeekDay=Time[0].DayOfWeek.ToString().Substring(4,8);
DrawTextFixed("day",theWeekDay, TextPosition.BottomRight);
DrawText("day"+Convert.ToString(ToDay(Time[0])),
theWeekDay+" "+theDate,0, Low[0]-TickSize*20, Color.Blue);
}
You haven't given enough information to solve your problem, but if you're just trying to get the day of the week name in the abbreviated format, use this instead:
theWeekDay = Time[0].ToString("ddd");
You're going to have to provide more than just this snippet of code. What is the Time object you're accessing via an indexer? Have you debugged this to see if Time[0] actually has a value? My guess here would be that Time[0] doesn't return a value that DayOfWeek can work with hence Substring(0,3) is being running against either an empty string or a null value
Unless you have omitted part of the code, your assignment does not take place within a class definition or a method.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I need to find the mentioned lambda statement and remove it from my code. My project is so big, and I noticed in the find and replace box of Visual Studio, there is an argument that can use regular expressions to find and replace codes. Is there a regular expression that can find this statement completely (contains line break and white space also)?
() =>
{
CallMethod()
},
I'm afraid that VS IDE is using the regular expressions in single line mode (which is actually strange considering that it offers \n in the suggestion menu). I think you will be a lot better creating a new project, which will load the file, read all text from it, and replace whatever regex you specify, and then save the file back.
Basically the regex you need is this:
(\(\ *\)\ *=\>[\r\n\s\{\}]*CallMethod\ *\(\ *\)[\r\n\s\{\}]*,)
In C# code, you can do it like this:
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(#"(\(\ *\)\ *=\>[\r\n\s\{\}]*CallMethod\ *\(\ *\)[\r\n\s\{\}]*,)", System.Text.RegularExpressions.RegexOptions.Multiline);
regex.Replace(document, string.Empty);
Hope this will be of help to you.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to eval() and run this javascript code from my C# program, but it won't even debug.
How can I do this?
string jsFunc = "eval(function(p,a,c,k,e,d){while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+c+'\\b','g'),k[c])}}return p}('8 4=\'6/13!)!6/12))6/19))))2!,!18*!16!15*!,!:14*-!17:9*!,!26***<\';8 5=\"\";20(3=0;3<4.24;3++){10(4.7(3)==25){5+=\'\\&\'}11 10(4.7(3)==23){5+=\'\\!\'}11{5+=21.22(4.7(3)-1)}};5;',10,27,'|||i|s|m|Nbui|charCodeAt|var||if|else|bct|spvoe|521|8477|_|73|2689|njo|for|String|fromCharCode||l{�ength|28|4451'.split('|')))";
JSEval.JSEval eval = new JSEval.JSEval();
string expression, result;
Console.Write("Выражение: ");
expression = jsFunc;
try
{
result = eval.Eval(expression).ToString();
}
catch
{
result = "!!!";
}
One potential problem, if I am permitted to hazard a guess based on the slim details available, is the odd character sequence found in the string:
...||l{�ength|28|4451'.split('|')))";
Perhaps you should remove the {� and re-run the code.
To elaborate on other meanings of the phrase "code don't debug":
Ensure the project is configured to build in Debug mode.
If your expectation is that you can step through the JavaScript, this will not be possible. You should instead debug the JavaScript using something like Firebug.
If you cannot mentally debug the JavaScript, because it has been minified, you should look at a tool to unpack the JavaScript into something more human readable.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Has been solved. Was trying to create a do statement that would not do anything until the while was checked. Was solved by simply putting an if statement before the do statement.
Using a do statement and anything else you might need, write the equivalent of:
while (b) {
s;
}
I think you might be looking for
if(b)
{
do
{
s;
}
while(b);
}
Is a standard part of the language:
do
{
s;
} while (b);
If I understood your question correctly you have a code that uses do..while loop and you want to replace it with a "regular" while loop.
That is not so simple as you say in your example. The difference is that do..while will always run at least once, and check the condition after it's completed. While loop may not run at all.