Do linq2sql expressions always return IQueryable? - linq-to-sql

Do linq2sql expressions always return IQueryable?
Can I return IList if I wanted?

I think this is the way to do it:
IList<SomeType> result = _someCollection.Where(...).ToList();

By returning an IList, you are evaluating the query. The beauty of Linq2Sql is that it translates your expression into a sql query and executes that against the server. By returning an IList, you lose this functionality.
There is nothing stopping you from calling .ToList() on your IQueryable object once you have created your query though.
Hope that helps

Related

(Spring JPA) Method name query

First of all, sorry for my poor english.
I want to change the following query to 'findBy~' method, but i don't know how to.
#Query(value = "SELECT t FROM Table t WHERE (b.num1 <= :variable or b.num1 IS NULL) AND (b.num2 >= :variable or b.num2 IS NULL)")
Or, is it impossible to get the result by using 'findby~' method name?
I would appreciate if anyone could reply.
Spring Data JPA does have support for all the conditions in your query and nesting of conditions. I'd argue that your query name will become unnecesarelly verbose. It would end up as
Table findByNum1LessThanEqualOrNum1IsNullAndNum2GreaterThanEqualOrNum2IsNull(Integer var0, Integer var1);
This should return the appropiate query, but you'd need to send the variable twice, once for each equals.
With #Query you have the freedom to call your query as you'd like and reuse the same variable.
Now, you CAN fix the downsides of using named methods by using a default method like
default Table myQuery (Integer var) {
return findByNum1LessThanEqualOrNum1IsNullAndNum2GreaterThanEqualOrNum2IsNull(var, var);
}
So you call this instead of the actual query, but then again, it would be much cleaner to use #Query with a proper, descriptive or even self-documenting name if you don't comment your code (you should comment your code). In any case, I suggest you use method names for simple queries and use #Query for anything more complex.
Please, refer to the following links for further reading:
Spring JPA Query Creation
Spring JPA Query Keyword Repository
LeafyJava article on Query Precedence Tricks, which also provides and example of how to change your query logic in case the conditions aren't arranged as you want.
This SO question also provides a bit of insight.

ISingleResult Cast in Code?

I have a sproc in my Linq to SQL (.NET 4.0) designer that returns the default ISingleResult. I wanted it to return something of Type "MyTable", so I made the change in the designer.
So this works:
List<MyTable> return = dataContext.MySproc.ToList();
However, I would rather cast/convert the return from the sproc in code to stay away from the designer. For some reason I can't get the trick of converting ISingleResult from the sproc into List. Can someone help me with the syntax?
Note, if this is easier in EF and someone can point me to HOWTOs using that toolset, I'm all ears.
Thanks.
Just pass your ISingleResult into the constructor of List.

How efficient is it to call a UDF and sproc from within my LINQ to SQL?

I ran into an issue where I need to call a UDF within my LINQ to SQL and then another stored procedure within that. Here's the code.
public IQueryable<DataDTO> GetLotsaData(string dataId, DateTime date, string custIDs)
{
var data = (from rs in _context.spXI_GetData(dataId, date, custIDs)
select new DataDTO
{
Time = rs.Time,
TimeZone = _context.GetTimezone(postDate, _context.GetDetailedData(rs.PKID, custIDs).FirstOrDefault().Zip),
CompletedTime = rs.Completed_Time,
});
return data.AsQueryable<DataDTO>();
}
The line I'm worried about is the one where I'm calling the GetTimezone UDF. Is it inefficient to call a UDF in the middle of a LINQ query and then another stored procedure (GetDetailedData) to get a single value for that UDF? What kind of SQL would this generate?
It looks a bit convoluted to me, but still better than the alternative which would be a sub-select or join in my stored procedure. (I'm trying to avoid having my stored procedure return the new field - TimeZone - instead just having it returned in my DTO.) And yes, I realize this could all be avoided if we were using UTC. Sadly, I have no control over that.
Why can't spXI_GetData return the complete result set? I'd say that would be optimal in this situation.
The GetTimezone and GetDetailedData functions will be called for every row in the spXI_GetData set. Would be better if the GetTimezone function could return a inline table and than you could join with it instead.

L2S, Caching, and error: The query results cannot be enumerated more than once

I have a fairly complex query (that includes a table valued function to allow full text searching) that I am trying to cache (HttpRuntime.Cache) for paging purposes. When I try to use the cached L2S query, I get the error stated above: The query results cannot be enumerated more than once.
I have tried assigning my query to another IQueryable object by calling AsIQueryable() on the cached object, but that does not help.
Any ideas?
You could store the results of the query in the cache instead of the query itself by calling .ToArray() or .ToList() extension methods which will execute the query immediately. Then you can enumerate the results from the cache as much as you wish.
use
var retVal = (.....).First() or ToList();
and use retVal.Name, retVal.Surname ....
if you use ToList();, you need to give an index like retVal[1].Name

Override SQL generated by LINQ to SQL?

Is it possible to override the SQL generated by LINQ to SQL, for optimisation purposes?
You could use the ExecuteQuery method instead. This is useful if you want to leverage a function that's available in SqlServer but not in Linq (IE PIVOT, etc...)
For instance:
var query = db.ExecuteQuery<MyType>( #"SELECT ... FROM ... WHERE ...");
One way I have used:
Create a stored proc, use the linq to sql designer to drag the proc into the design surface. Call the resulting method instead.