This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product. Otherwise - post away!
Hello I am trying to run this relatively simple linq query but I am finding the ternary statement is not supported.
IQueryable<MyEvent> myEventQuery = from o in myEventsCollection where
[NotSupportedException: This expression type is not supported in LightSpeed 3.0]
It is very urgent. Many thanks in advance.
Unfortunately we dont currently support the ternary statement in our LINQ provider.
You could however look at a different approach for your original query where you use a switch statement to append the appropriate additional criteria assuming this fits with my understanding that day is a local variable:
public IQueryable<MyEvent> GetEventQuery(DayOfWeek day)
var query = from o in myEventsCollection where o.ContactId == ContactId select o;
query = query.Where(o => o.Monday == true);
Would that fit for your scenario?
I see how it would work, unfortunately would that not create two database queries? - And using this query in a loop so that may be some unneccessary overhead. I have got it to work by converting my Events collection to a List<MyEvent> before the loop and can run my original query.
Is there any plans to include ternary at any point?
Thanks for your help,
No, it won't create two database queries. Recall LINQ query execution is deferred until you do a foreach or ToList or whatever. So the first line of Jeremy's code merely creates a query object -- it does not run the query against the database. Then his switch statement adds a clause to that query object. At some later point, this expanded query gets foreach-ed or ToList-ed or Count-ed or whatever, and THEN it hits the database -- with the WHERE clause added by the switch in place.
Thanks Ivan for clearing that up. Thats good to know - I think I will need to get a book on linq to get a proper understanding, I am sure I am not using it enough! Thanks again for all the hard work you, and the rest of the team put into this product, I honestly can't think of a programming tool I value so much - and I can't imagine database programming without it.