- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
You should be able to construct a similar query in LINQ by incorporating Criteria into the where clause. Lambda expressions are closures, right?
In practice, you should almost never need to generate a query dynamically unless you are writing a developer tool or your database sucks.
Admin
Admin
Well, I don't know if you're right about C#, but never fear, Java is long since over that "what you see is what you get" phase.
Every time I get a new CDI/Weld injection error I hear a little voice in my head: "Yo Dog! I put a proxy on your proxy, so you can proxy while you proxy".
Every once in a while I look around at where JEE is heading and wonder if I'm the only one with any common sense, or if I've just lost it.
Admin
Admin
What you've done is used lambdas to search an object collection which gives you way more flexibility than using lambdas in LINQ to SQL because when you're writing LINQ to SQL you need to ensure that the underlying SQL is actually efficient and doesn't include all sorts of superfluous checks. Have you ever actually used LINQ to SQL or did you simply assume you could use the same convoluted lambdas as you do with LINQ to objects? I made the LINQ to SQL requirement perfectly clear when I wrote my original missive, as too the logic I was trying to create in the underlying SQL.
And besides this, you have totally failed in another core requirement - to build up the query sequentially. I said the example was dramatically simplified and it is because in the example you have the whole criteria object up-front. In the real app you don't this luxury. This is exactly why I told you, several times, that the query had to be built up sequentially based on conditional logic. You have failed to do this and your argument is "I have no experience building expression trees this way". Er, no, you can't build expression trees this way, ie. by using lambda statements. That was the entire point of my original post and exactly the issue you were trying to contest!!! Yet now you tell me you don't actually know how to? So why the hell are you trying to contest my point when you don't even know how??!! I made the requirements perfectly clear, you obviously misunderstood them and it is obvious you were flat out wrong in your assertion that my requirements could be fulfilled with lambdas. Or maybe you weren't, but either way you have no clue how to do it.
Sorry my friend, I mean you no disrespect but the simple fact of the matter is that you have TOTALLY failed to meet my requirements and dispute my orginal point. I also think it's cute how you say "I have no experience building expression tress..." because this was exactly my original point about syntactic sugar!!! You can't physically do it with the syntactic sugar and you don't know how to do it any other way because you don't understand the underlying classes. I, on the other hand, do, which is why I can easily fulfil these requirements through direct manipulation of the underlying classes. Score one for actually understanding the classes and a big fat zero for syntactic sugar.
If you want to keep this going I'm happy to oblige but so far you've only managed to prove your ignorance. I mean no offense by that, just stating a fact.
Admin
I've never used lambda expression in the way you're describing, but they are quite useful for inlining anonymous functions for IO and simple multi-threading applications. Also, making use of local variables is a neat trick.
Admin
Hmmm, ok let's go over the conversation, and I'll dumb down how it went for you.
You: YOU CAN'T DO (A AND B) OR (C AND D) WITH LAMBDA STATEMENTS! ("That is not a complicated query by any stretch of the imagination but it is physically impossible to express with lambda statements.") Me: Yes you can (show example code). I must be missing something. You: OMG I SAID SEQUENTIALLY, AND YOUR CODE IS TEH SUX0R. BESIDES I WANT TO USE CLASSES! Me: It's test code, here's some other test code that uses classes. You: YOU NEED TO DO THIS!!! ("Please satisfy these requirements using lambda expressions") Other guy: You can do this in SQL by doing -X- Me: I don't have experience with expression trees, but here is the logic requirements -X- using lambdas You: OMG THE SQL IS HORRIBLE, DIDN'T YOU ATTACH A LOGGER FOR THIS TEST EXAMPLE CODE YOU CREATED FOR MEEEE?! YOUR CODE SUX0RS AND YOU SUX0R.
The point I was trying to contest was actually the "you can't do (A and B) or (C and D) with lambda statements." I was trying to give you a working solution using lambdas, and I have. The fact that you think they don't follow the logic requirements is unfortunate, because someone else came up with the same solution in SQL. You obviously are too proud to be on the internets, so I'll leave you be.
Forgive me if I don't believe you.Admin
No problem mate, glad you learnt something today. If you ever actually figure out how to do this I'd love to hear back from you but trust me you won't, because it simply isn't possible with the syntactic sugar. Which is, of course, exactly the point I was making in my original post.
No hard feelings, have a good one.
Admin
If what you want is the most efficient sql, then you do not want a sql condition of where ( (SourceId = Critieria.SourceId [AND SourceName = Criteria.SourceName]) OR (TargetId = Criteria.SourceId [AND TargetName = Criteria.SourceName]) ) at all. You want to "select where the first condition ... union select where the second condition". Trust me, it will run faster. And if you're doing something that is really hard to do in Linq, why not just do dynamic sql in your database itself? It can be a reasonable solution in the few exceptional circumstances where it's actually needed. Most of the time it's used is a mis-use, but there are legitimate cases. At no point did your requirements mention that this sequential query building be done in .net at all, so why are you using the tool this way?
Admin
Admin
What I'm doing is not hard to express in LINQ, in fact it is very, very easy - the diffcult part is getting that LINQ to write decent SQL!! I admit it was my choice to use LINQ to SQL because I wanted to get my hands dirty with a new technology but I don't for one second think it was an inappropriate choice of technology for the job. This was a really trivial little database app that should have been a breeze to implement with LINQ to SQL. This is exactly what LINQ to SQL was designed for so why on Earth would I not expect it to be an adequate solution to the problam at hand?
Admin
Admin
This is something I fixed up. I know there's probably a bug with something. I have not TESTED it, and it's 3 AM so I wont.
I'm 100% sure there's a better solution for this... I'm just too tired.
Anyway, I've created two methods, one with a union and one with a Where clause. I've got a list of Criterias (yeah...) and I'm checking it with SomeCriteria.
BTW There is a way to dynamically combine Labmdas.
You can use something like Func<T,TResult>.Combine(Func1,Func2), I'm not sure how it'll work out though, it'll probably blow up.
You can also try something like this:
Func<Something,bool> f1= (Something s) => s.foo == bar Func<Something,bool> f2= (Something s) => f1(s) && s.bar == baz
Hope this was a helpful first post...
Admin
I notice you're working directly with predicates in the form Func<T, bool>. This is similar to the approach I came up with (using some guidance from the excellent PredicateBuilder class, google it if you want to take a look). By working directly with predicates and the Expression class, I was able to quite easily create AND and OR relationships between existing queries, similar to this:
In the above code you'll notice that the actual magic is performed by the LambdaAnd and LambdaOr methods, which are as follows:
So, even though we need to get down and dirty with the class that underlies lambda expressions (that class is Expression<Func<T, bool>>) it's actually a very simple means of building up AND and OR relationships between existing queries. I think you were coming from a similar direction but you can make your code even neater if you look into the Expression<Func<T, bool>> class rather than working simply with Func<T, bool>. The Expression class contains a whole wealth of methods for creating pretty much any logic you want.
Thanks again, I look forward to seeing more from you in TDWTF comments.
Admin
Admin
Admin
If you know the right way to do this, why don't you use the right tool? LINQ to SQL is not the right tool for dynamically built sql with variable conditions.
Admin
I'm just sayin'.
Admin
I'm just here for the flesh.
Admin
I really can't make it any clearer for you. Using an ORM over a SQL database is exactly the right tool for the job of creating a simple SQL database application. It is exactly what they're designed for!!! LINQ to SQL was the right tool for the job and it truly worked wonders, as soon as I dropped the syntactic sugar and got to grips with the actual guts of LINQ.
Admin
Still, thank you. I was waiting for someone to point it out. Getting your hands dirty for experience is one thing, but in the end if you know how to write it clearly in a few lines of SQL, just do that. Developers that inherit your code will thank you for it.
Admin
But hey, I was only being sarcastic, I'm sure he's a pretty smart guy. Ha, there I go again! But seriously folks, we all know it's implied that nothing good ever comes out of these comments...
Admin
I also have to stress once again that LINQ to SQL worked a treat, at no time have I denigrated it because it did the job perfectly - as soon as I dropped the syntactic sugar and started working with the underlying LINQ classes. I know my team will be able to inherit this code and go straight to work on it, something they couldn't readily do if half of it was coded as SQL in the underlying database (yeah I know, I need a better team, but when life gives you lemons...).
Admin
Still, I don't see your justification, other than purely personal taste on the part of your team (and for your own experience).
Ok, you've convinced me that my comment about developers being happy to inherit SQL was wrong. Also, I'm glad you can admit there's something wrong with a team of developers working on an application that queries a database refusing to write code that actually queries a database.Admin
No, you are correct. But Date APIs only suck because dates themselves (as implemented by mankind) suck.... plus the date systems tend to get changed every few years (to adjust for leap-seconds or the king's birthday or whatnot), instantly making all existing Date/time API code buggy.
The only proper way to implement date/time is femtoseconds-since-big-bang (uint128). Everything else is a fragile hack.
Admin
I have the same thing in some security code I maintain, it's a fairly obvious comment on a potential gaping security hole in the code, inserted to see if any of the "I only trust security code if it's OSS" crowd will notice. So far no-one has, but then it's only been twelve years...
Admin
stinkaaaaaa ffs yea.
Admin
No, it's measured in milli-MUMPS.
Admin
I'm just here for the ambience. This is the venue for the wine-and-cheese isn't it?
Admin
on some older versions of PHP, it actually was that bad:
time php -r 'var_dump(strtotime("NOW + 40000000000 DAYS"));' bool(false)
real 0m14.337s user 0m14.337s sys 0m0.000s
Admin
Don't laugh. I can actually do that. My home PC is a ten year old Compaq.
Admin
Dammit! This was supposed to be a response to the guy who was listening to his pc! WTF!
Admin
Well, for Persian, you'll just need to tweak the month lengths. Ethiopic will require a 5DayWheel, and depending on what variant of Saka you need, you might have to have a 32DayWheel.
</deadpan>Admin
No, he bailed the second you continued to treat him like shit and thus demonstrated that your value was so low that to continue interacting with you would only serve to drag him down with you.
You say no hard feelings, but your entire comment was making fun of him. And now he has the upper hand, since he can easily claim that he didn't continue to help you not because of lack of knowledge but because you are a shitty person who doesn't deserve the help.
Merry Christmas, asshole.
Admin
NO I DON'T CARE IF THE LAST MESSAGE IN THIS THREAD IS FROM 2010 FUCK THE POLICE