|
GrumpyDoctor posted:Was the old behavior originally intended, or was it a bug? That behavior seems incredibly counterintuitive to me, especially because, if I understand right, there's no way to explicitly request it anywhere in the language. C# code:
I'm glad Microsoft "fixed" this behavior to do what a normal human being would expect. Which is to have each Where clause capture the value of keyword by using the temporary variable. Also, I didn't realize multiple Where clauses were cumulative (is that the right term?). I learned something new. EDIT: VVVV It totally makes sense. I just never thought of it that way. At least Ithaqua didn't realize it. I take solace in that gariig fucked around with this message at 17:12 on Sep 16, 2014 |
# ? Sep 16, 2014 17:04 |
|
|
# ? May 18, 2024 14:51 |
|
Where clauses are normally cumulative, so why would Linq-to-Entities/Linq-to-SQL be any different?C# code:
|
# ? Sep 16, 2014 17:09 |
|
gariig posted:
Well, yeah, but we're not compilers Score another point for immutability-by-default, I guess.
|
# ? Sep 16, 2014 17:37 |
|
Here's some more info about closures and loops.
|
# ? Sep 16, 2014 17:51 |
|
[edit] dumb
New Yorp New Yorp fucked around with this message at 21:18 on Sep 16, 2014 |
# ? Sep 16, 2014 19:51 |
|
Sedro posted:
You guys are way too good to me. That took care of that problem. However, that leads to Another .NET problem (or possibly an sql problem) I'm downloading these images, and then I'm updating the database with them so the item knows what it's image url is. However, I'm getting this message around halfway through*: code:
code:
* Halfway being about 10,000 rows; There's only about 25,000 rows in the database >total<
|
# ? Sep 16, 2014 20:45 |
|
Back to our LINQ chat, I did a quick test, and the way I thought it worked seems to be the way it actually works. I think I confused myself earlier. Nevermind.
|
# ? Sep 16, 2014 21:17 |
|
Ithaqua posted:I'd honestly never considered it before. I always assumed that multiple Wheres in LINQ to Objects wasn't cumulative, so doing that would iterate the list once, and give you 1,3,4,5. Then it would iterate the second list and give you 1,3,5. Of course, now that I think about it, it makes sense that it would be able to be optimized so the list is only iterated a single time. It makes even more sense when you consider that LINQ to Entities doesn't actually come into play until you enumerate the query (.ToList() or otherwise). Until then, you're just building an expression tree that LINQ to Entities will eventually parse.
|
# ? Sep 16, 2014 21:18 |
|
Bognar posted:It makes even more sense when you consider that LINQ to Entities doesn't actually come into play until you enumerate the query (.ToList() or otherwise). Until then, you're just building an expression tree that LINQ to Entities will eventually parse. Yeah, LINQ to Entities is a different story. I'm sure it doesn't generate any SQL until you run the query. I think I was having a brain spasm at the line query = query.Where. I didn't read it as compounding LINQ methods on top of the original query, I just read it as reassigning the last method on top of the original. Dumb on my part. I retract everything I've ever said.
|
# ? Sep 16, 2014 21:21 |
|
Ithaqua posted:I'd honestly never considered it before. I always assumed that multiple Wheres in LINQ to Objects wasn't cumulative, so doing that would iterate the list once, and give you 1,3,4,5. Then it would iterate the second list and give you 1,3,5. Of course, now that I think about it, it makes sense that it would be able to be optimized so the list is only iterated a single time. It's usually safe to think of Linq iterating the list and producing a new list each time. Though with infinite lists, the laziness is no longer an optimization and actually necessary for correctness. For example you could create a random number generator, then transform it to get random numbers which are even. C# code:
C# code:
|
# ? Sep 16, 2014 21:24 |
|
Scaramouche posted:So the idea being, the stringbuilder can only get up to 1000 rows before it gets sent off to the ExecuteSql function (where the error occurs), and the ExecuteSql after the loop will only catch the last <1000 rows. Is 1000 too much, or is this somehow occurring in the same scope/operation despite the batching? The InsertOrUpdate proc is a basic one like (from memory) select id from table where id=@id if @id is not null then update blah blah blah. Grab the ErrorCode from the OleDbException and Google around for that. Maybe also inspect the Errors property for additional information.
|
# ? Sep 16, 2014 21:29 |
|
Ithaqua posted:I assume that's a C# 5 compiler thing, so it doesn't matter which framework version you're using, right? Correct, it's a compiler thing and works on all target frameworks. GrumpyDoctor posted:Was the old behavior originally intended, or was it a bug? That behavior seems incredibly counterintuitive to me, especially because, if I understand right, there's no way to explicitly request it anywhere in the language. (Also, how did I not know that?) The old behavior was just an oversight. The difference is only observable with lambdas. Prior to lambdas, there was no way to distinguish. When the compiler team added lambdas we just didn't even notice this one corner until it was too late.
|
# ? Sep 16, 2014 22:57 |
|
New problem I'm afraid. I want to do the following excel SUMPRODUCT calculation in C# and I'm not sure how to go about it within the current confines of my code. code:
code:
code:
M, N and R are all stored in 2 dimensional arrays, based on the current year and month (M being SRCF, N being SRDiscFact and R being PrudentP). code:
code:
A Tartan Tory fucked around with this message at 15:04 on Sep 17, 2014 |
# ? Sep 17, 2014 12:26 |
|
Are you trying to calculate SUMPRODUCTs after you've already got the arrays, or do them on the fly as you generate the arrays? Your code is dense and your explanation is unclear. If it's the former, this function will get you what you want (without the division in the spreadsheet):C# code:
|
# ? Sep 17, 2014 15:33 |
|
GrumpyDoctor posted:Are you trying to calculate SUMPRODUCTs after you've already got the arrays, or do them on the fly as you generate the arrays? Your code is dense and your explanation is unclear. If it's the former, this function will get you what you want (without the division in the spreadsheet): The arrays with the values the SUMPRODUCT is using have already been calculated, so I'll try this and see if I can get it to work, thanks! Edit: Yeah I can't get this to work, it's displaying close numbers but they aren't correct and are going up instead of going down. Is there something I am missing? Are you sure I don't need the if/else statements? code:
Also I'm trying to avoid multiple methods because they are confusing the gently caress out of me, really new to this, so doing it in the same method. I know it's stupid and dirty, but it's the only way I can understand atm. Further edit: Have moved all the values into single dimension arrays in an attempt to understand what I am doing wrong. Still not getting it to work. code:
A Tartan Tory fucked around with this message at 16:44 on Sep 17, 2014 |
# ? Sep 17, 2014 15:40 |
|
A Tartan Tory posted:Also I'm trying to avoid multiple methods because they are confusing the gently caress out of me, really new to this, so doing it in the same method. I know it's stupid and dirty, but it's the only way I can understand atm. You really need to understand methods in order to write sane, maintainable code. What don't you understand about them? Part of the problem that you're experiencing right now is that you've created a brittle tower of code that you don't quite understand, so anything you change just breaks it all and makes the problem worse. Give a small example data set and the expected results and we can help you, your code is difficult to follow.
|
# ? Sep 17, 2014 16:49 |
|
Oh, you don't just want the SUMPRODUCT. You want to do something with it. That's my bad; I didn't read your Excel formulas closely enough. But that's because I'm not great at Excel. Can you describe your problem without any technical concepts or code? (SUMPRODUCT is ok.) Pretend you're explaining to someone completely non-technical.
|
# ? Sep 17, 2014 16:56 |
|
Ithaqua posted:You really need to understand methods in order to write sane, maintainable code. What don't you understand about them? I know, in the actual main code section it's a tiny bit more readable, but any tips you have on making better methods would probably help out. GrumpyDoctor posted:Oh, you don't just want the SUMPRODUCT. You want to do something with it. That's my bad; I didn't read your Excel formulas closely enough. But that's because I'm not great at Excel. Can you describe your problem without any technical concepts or code? (SUMPRODUCT is ok.) Pretend you're explaining to someone completely non-technical. Ok, basically what I want to do is this. 1. Pre-populate three arrays, based on the current year and month. (done) 2. Use those arrays to do a sumproduct, but to do it for every year and month in another loop so I can get the sumproduct for every y, m. (note the initial y, m is the next months array value, so for y0m0 it would start with the values from y0m1) 3. Use the sumproduct for every y, m and divide it by two current values also in an array based on y, m. 4. Save the final value in an another array based on the current y, m for future display. I'm getting a little bit close to what I want, the values aren't completely correct but they are close and actually going down in value which is what I want. code:
A Tartan Tory fucked around with this message at 17:07 on Sep 17, 2014 |
# ? Sep 17, 2014 17:04 |
|
If someone calls SomeClass SomeClassInstance = SomeClass.Load(key); with an invalid key, should the method return null or throw an exception or log some swear words or what? As far as I can tell I could go either route without much headache but I'm sure that this is something that has lots of established best practices. fake edit: Please, look inside your mercy file... File Not Found.
|
# ? Sep 17, 2014 17:07 |
|
A Tartan Tory posted:Ok, basically what I want to do is this. Too technical. I'm an executive VP who's never programmed in my life. I don't know what "array" or "loop" means. Y? M? Just use English! (I'm not just being a stickler to be an rear end; your fogginess about how to solve the problem is directly related to your fogginess about explaining it, I promise you.)
|
# ? Sep 17, 2014 17:08 |
|
Newf posted:If someone calls SomeClass SomeClassInstance = SomeClass.Load(key); with an invalid key, should the method return null or throw an exception or log some swear words or what? As far as I can tell I could go either route without much headache but I'm sure that this is something that has lots of established best practices. With that little contextual information, I think the only advice anyone can reasonably give you is "All of those options are fine, as long as whatever you do is consistent with the rest of your codebase." e: If there are multiple reasons that a key can be invalid, don't return null, because that hides the reason for the problem.
|
# ? Sep 17, 2014 17:10 |
|
GrumpyDoctor posted:With that little contextual information, I think the only advice anyone can reasonably give you is "All of those options are fine, as long as whatever you do is consistent with the rest of your codebase." Logging swear words it is, then.
|
# ? Sep 17, 2014 17:11 |
|
GrumpyDoctor posted:Too technical. I'm an executive VP who's never programmed in my life. I don't know what "array" or "loop" means. Y? M? Just use English! Ah right, let me try again. 1. Pre-populate three lists with single values, base the lists on the current year and month, with every value corresponding to a specific month. 2. Use those lists of values to do a sumproduct, but to do it for every month so I can get the sumproduct for every month. (the month starts by using the next months list value, so for the first month in the first year, it would start with the values from the second month in the first year until it got to the eleventh month in the second year, the second month in the first year would use the values from the third month in the first year until the eleventh month of the second year etc etc, the eleventh month of the second year would just use it's own list numbers for that month, where any month after that would start from the eleventh month of the second year for it's sumproduct and end at the current month's values) 3. Use the sumproduct for every month generated and divide it by two list values from that month, in order to get the final value. 4. Save the final value in an another list based on the current month for future display. Is that a little better? A Tartan Tory fucked around with this message at 17:21 on Sep 17, 2014 |
# ? Sep 17, 2014 17:17 |
|
Newf posted:Logging swear words it is, then. Want to do this so bad sometimes.
|
# ? Sep 17, 2014 17:59 |
|
Newf posted:If someone calls SomeClass SomeClassInstance = SomeClass.Load(key); with an invalid key, should the method return null or throw an exception or log some swear words or what? As far as I can tell I could go either route without much headache but I'm sure that this is something that has lots of established best practices. I would expect any static methods returning an instance of the class (essentially fancy constructors) to throw an exception on failure unless it is explicitly clear from the name that they may return nothing as a normal part of their operations. Note that the latter is entirely separate from failing and you need to make it clear what is a failure and what is a normal "nothing result" situation. Under no circumstances should an unexpected failure result in returning null. For example, we might try to load a configuration section that may or may not be present and both are OK situations - returning null is appropriate in this case if the method explicitly expresses this. I generally prefix such methods with Try to make it obvious that maybe they won't return anything, e.g. ConfigSection.TryLoadSection(). However, if the configuration file itself is missing then TryLoadSection() must still throw an exception - this is an unexpected failure and not the scenario in which it is OK to return nothing. In short, you should never return null for failure but if you make it very clear that a "nothing" result is a normal part of the method, you may return null. In practice, my preference for marking such methods with a Try prefix is a rarity and most people seem to not place such a requirement. I find it very helpful for fast code comprehension, though. Logging is a completely orthogonal concern. Signaling errors in code should not be related to the existence or non-existence of logging statements. Add logging where it might be useful but do not use it to justify cutting corners on software design. EssOEss fucked around with this message at 18:21 on Sep 17, 2014 |
# ? Sep 17, 2014 18:18 |
|
EssOEss posted:For example, we might try to load a configuration section that may or may not be present and both are OK situations - returning null is appropriate in this case if the method explicitly expresses this. I generally prefix such methods with Try to make it obvious that maybe they won't return anything, e.g. ConfigSection.TryLoadSection(). However, if the configuration file itself is missing then TryLoadSection() must still throw an exception - this is an unexpected failure and not the scenario in which it is OK to return nothing. A Try method that throws an exception is just asking for confusion. If you name something Try[whatever] you should match the .NET framework pattern of bool TryGetWhatever(object parameters, out object output). EDIT: To answer the original question, throw Exceptions on exceptional cases. Is it expected that the Load method could fail in normal operation? Could you realistically do something about it if it did fail? If so, return null or some other error descriptor. Does failing to load only happen in exceptional cases (e.g. file not found, couldn't connect to server, etc.)? Throw an exception. Bognar fucked around with this message at 22:27 on Sep 17, 2014 |
# ? Sep 17, 2014 19:36 |
|
A Tartan Tory posted:Ah right, let me try again. No, I still don't understand what you are trying to do. Pretend I don't know what you are trying to calculate and describe the process. Also, add a couple of small examples of input and output. Maybe even the manual steps to get your input to be output
|
# ? Sep 18, 2014 00:54 |
|
A Tartan Tory posted:Ah right, let me try again. Ok, I stared at this for ten minutes and tried to make some sense of it. Tell me if this is right: You've got three lists of values: M, N, and R. Each list has X values, and each value corresponds to data for a particular month x. You want to create a new list of values P, and then create a second list of values Q such that Q(x) = P(x) / ((N(x) * R(x)). Q is your output. The trouble you're having is with generating P. The tricky bit is that the way to calculate P(x) changes depending on x; if x < 23, you do one thing, and otherwise you do a different thing. If x < 23, then P(x) = SUMPRODUCT(M[x+1 to 23], N[x+1 to 23], R[x+1 to 23]), and if x >= 23, then P(x) = SUMPRODUCT(M[23 to x], N[23 to x], R[23 to x]).
|
# ? Sep 18, 2014 00:59 |
|
This sounds like something that can be solved with a few beautiful lines of LINQ, but the hardest part is going to be understanding what the hell is going on.
|
# ? Sep 18, 2014 05:12 |
|
gariig posted:No, I still don't understand what you are trying to do. Pretend I don't know what you are trying to calculate and describe the process. Also, add a couple of small examples of input and output. Maybe even the manual steps to get your input to be output I think this might be better if I present the excel problem visually from how I usually do this calculation. GrumpyDoctor posted:Ok, I stared at this for ten minutes and tried to make some sense of it. Tell me if this is right: This is basically pretty much what I want to do. Although it is (P(x) / (N(x)) / R(x) to get the value of Q. I'm hoping these screenshots make it a little easier to understand exactly what the calculation is doing (note all the values are changed to dummy values, obviously can't give out real stuff). This is the calculation on the first year of the first month. The blue, red and purple columns have already been pre-generated (as either a single or 2 dimensional array, I have them in both for practice) with the correct data for every month of every year needed, so has the current months data in the light green and pink entries. It's just a matter of accessing it so I can populate the SR array with the final values. This is the calculation on the first month of the second year, this time it uses less values in the sumproduct calculation, as it is getting closer to the end of the second year. Calculation when it reaches the changeover to start at line 37. Calculation after it has passed line 37, note how it now *starts* there for the sumproduct calculation and ends at the current month + 1. So basically, what I need to do, is work out the sumproduct of the blue red and purple columns every month, and use the sumproduct by dividing by the current months green and then pink values. Hope this helps a bit in the understanding part, I thought pictures would describe it better than I could. A Tartan Tory fucked around with this message at 10:05 on Sep 18, 2014 |
# ? Sep 18, 2014 09:15 |
|
I've just come across something I don't understand.C# code:
What gives?
|
# ? Sep 18, 2014 15:08 |
|
It has to be the fully qualified type names or missing usings. Does it work with just foreach(SettingsProperty p in ...)?
|
# ? Sep 18, 2014 15:18 |
|
A Tartan Tory posted:I think this might be better if I present the excel problem visually from how I usually do this calculation. As far as I can tell, this does something close to what you wanted: https://dotnetfiddle.net/WVUbMR The boundary conditions are probably wrong since I still don't think I fully understand those, but I believe the general idea is correct.
|
# ? Sep 18, 2014 15:21 |
|
Newf posted:I've just come across something I don't understand. You'll see that kind of thing a lot with pre-generic collections. It implements IEnumerable, not IEnumerable<T>. Even if all of the stuff in the collection is of the same type, the compiler has no way to know that. Just put a .Cast<Type>() in there. DataTable has the same problem. Even though its Rows are DataRows, the Rows property is a non-generic collection.
|
# ? Sep 18, 2014 15:41 |
|
Bognar posted:As far as I can tell, this does something close to what you wanted: I take it that is done in LINQ? Not overly familiar with it, but I'll take a look once I'm back at home later. Is there a way to do it with just basic loops? (edit: answering my own question after looking at it a bit further, I think I understand whats happening there, will update later if I get it to work!) Thanks for your efforts, I really appreciate it! A Tartan Tory fucked around with this message at 16:08 on Sep 18, 2014 |
# ? Sep 18, 2014 15:58 |
|
zokie posted:It has to be the fully qualified type names or missing usings. Does it work with just foreach(SettingsProperty p in ...)? If I stick in the using System.Configuration; then I can use foreach(SettingsProperty p ...), but still not var. Ithaqua posted:You'll see that kind of thing a lot with pre-generic collections. It implements IEnumerable, not IEnumerable<T>. Even if all of the stuff in the collection is of the same type, the compiler has no way to know that. Just put a .Cast<Type>() in there. Ahh, this makes sense. But my confusion has shifted to why I'm able to declare the inner variable type at all - having just tried, I'm able to compile and run C# code:
Isn't the whole point of strong static typed languages to avoid this sort of problem? Is this an edge case wart that was a side effect of the introduction of generics which we're happy enough to suffer? (Or an edge case that appeared after the introduction of both generics and the var keyword?) Newf fucked around with this message at 16:36 on Sep 18, 2014 |
# ? Sep 18, 2014 16:32 |
|
Newf posted:Isn't the whole point of strong static typed languages to avoid this sort of problem? Is this an edge case wart that was a side effect of the introduction of generics which we're happy enough to suffer? (Or an edge case that appeared after the introduction of both generics and the var keyword?) This is the problem that generics solved, but the collection you're iterating over doesn't have a generic interface, which is why var's type inference isn't working.
|
# ? Sep 18, 2014 16:40 |
|
Newf posted:
Java code:
|
# ? Sep 18, 2014 16:47 |
|
GrumpyDoctor posted:This is the problem that generics solved, but the collection you're iterating over doesn't have a generic interface, which is why var's type inference isn't working. I get that, but I don't get why I'm able to declare the type of a variable when the compiler doesn't know it ahead of time. It feels like a stricter version would force me to expect only objects from an IEnumerable and then cast them myself - at least in this case the cast becomes explicit. Had I gone with foreach(SettingsProperty ... instead of var in the first place I would never have noticed that any cast was taking place - that's bad, isn't it?
|
# ? Sep 18, 2014 16:49 |
|
|
# ? May 18, 2024 14:51 |
|
http://stackoverflow.com/questions/3917129/foreachderived-obj-in-new-listbase is your exact question with the answer (tldr it's a special thing that foreach does for historial reasons)
|
# ? Sep 18, 2014 17:39 |