Convert Session Object to IOrderedQueryable<T> - linq

I need to convert a Session object to an IOrderedQueryable and came up blank. I've thought of creating a wrapper, but its not working properly. Basically, I am pulling a Linq query and would like to store it so that I don't have to pull it each time I visit. There are up to 7-10 parameters per user so it's not something that's great for caching.

I can simply cast my Session object as an IOrderedQueryable like:
(IOrderedQueryable<T>)Session["myObject"];

It seems you want to store the data returned by the linq query, if that's the case you need to make it grab the data i.e. by using .ToList() and storing that.

Related

Save Related Documents In Mongo Reactive But Not In The Same Collection

I would like to know, how to save related documents in reactive mongo ?. Because I find a code that attempts to do the magic... But when it should save the related document in another collection, it serializes inside the "father" of the relationship let say... I know that in spring data reactive mongo, #DbRef doesnt have support... How could I save the data in a way that, if I query the collection, I see that the attributesare the name of the collection and the generated id instead all of the object attributes ?.
If the pic above is seen, you will see that the attribute "user" is saved as a nested document but not in the corresponding collection. Do I need to hook in another event ?.
I had put a listener onbeforeconvert to scan every time a save operation is to be applied and save the object... How to proceed ?... think I should verify if it has a related doc from another collection and if its nonnull... If the object doesnt have any attr l
Alike, then save it... if not continue the scanning... dunno

Saving an object if not exists without using ObjectId (JS)

I'm trying to find a way to save a new object if one like it doesn't already exist with a specified custom ID.
I basically need to refresh a whole bunch of data every day but each item has its own ID set somewhere else (so not a parse objectId). I therefore can't use something like the standard save function as it'll just make a new record every time.
I've thought of trying to set the objectId of an object when its first created in the DB but I can't seem to do that... (Half expected this).
I've also thought of doing the obvious - checking if an object exists using a normal query but with a few thousand objects to compare, this will be very inefficient.
Any suggestions?
Maybe there is a save function or variation where its kind of "save or create depending if an object exists with this value in this field" :)
Thank you!
PS. I'm writing this within a Cloud Job - so JavaScript

Entity Framework returns different result with AsNoTracking

I use Entity Framework in combination with an Oracle database. If I create a query like
myLinqStatement.ToListAsync()
I get wrong data returned as a result. If I change the statement to
myLinqStatement.AsNoTracking.ToListAsync()
I get the correct data.
I also checked the native SQL query, which is generated by myLinqStatement.ToListAsync(). The generated SQL query is correct, because I get the correct data.
So is there a problem in the mapping? And why is it working with AsNoTracking?
Thanks!
What AsNoTracking does is to retrieve the data without attaching it to the context, hence any changes you apply over the data do not take effect unless you attach it again so that EF knows it should track its changes.
The code snippets you've provided do not show the whole picture, from the moment a context is created, but is it possible that other parts of your code mutate data before you call myLinqStatement.ToListAsync()?
As you mention that myLinqStatement.AsNoTracking.ToListAsync() returns expected data, makes me assume that there are some side effects in your code that AsNoTracking simply is not aware so just returns whatever it finds in your db
I came across this question because I had a similar issue with Entity Framework Core querying a DB view, the issue was cause because view didn't have a key defined, after defining a key for the entity that map to that DB view, the query returned the same result in both cases (using AsNoTracking or without using it).
In T-SQL a key for a DB view can be defined this way:
CREATE UNIQUE CLUSTERED INDEX UQ_MyDBViewName_ColumnKey
ON dbo.MyDBViewName (ColumnKey);
And in code, you can map the key using the [Key] attribute in the corresponding property of the entity or using the EF fluent API. It will depend of what the project is using.
Either way, using AsNoTracking on a query that goes directly to a DB view makes a lot of sense. Also, if for some reason the query of the view does not allow us to define a key for that view, then the option is to use AsNoTracking.
Hope it helps anyone else having the same issue.

Getting a dbid by table name

As far as I know, all QuickBase API calls are called using the following syntax: http://<quickbase>/db/<dbid>?
Is there a way to get the dbid field without navigating to that database table within QuickBase?
If the above is not possible, would anyone recommend anything other than creating another table that stores the IDs of the tables you want?
With the latter method, I believe I would only need to store one dbid and could pull down the rest (which I believe would still need to be user entered, but would be better than requiring them to change the code).
Thanks for the help!
API_GetSchema will return the list of dbids.
https://www.quickbase.com/db/<yourApplicationId>?act=API_GetSchema&apptoken=<yourApplicationTokenId>&fmt=flat

Linq to objects not "persisting" changes

I am using linq to select a bunch of objects by Id through a facade object. This facade has a function GetObjectById(string id) which returns a MyObject. I select a bunch of object in one query based on a list of ids:
IEnumerable<MyObject> objects =
from id in ids
select facade.GetObjectById(id);
Then later I set some value on my objects like so:
foreach(MyObject object in objects)
{
object.someValue = "banaan";
}
Later when I inspect the objects IEnumerable, someValue is not set to "banaan".
It looks like it has something to do with the deferred execution of linq because when I put .ToList() behind the first query I does work. However, I thought I should be able to do something like this. What am I doing wrong here? Or is my understanding of how you should use linq wrong?
Thanks in advance.
Sure it is the problem of deferred execution. If the query is executed again it will again call your facade method during and if the facade method loads data from somewhere (= create new instances) your former changes are lost. Your foreach is first execution and inspection outside of the first foreach is second execution.
You must call ToList if you want to operate on the same instances in such scenario.
When you inspect the objects the second time, do you re-evaluate the objects enumerable? Because that would make it call facade.GetObjectById(id) again.
Try to materialize the enumerable instead. Something like this:
IEnumerable<MyObject> objects =
(from id in ids
select facade.GetObjectById(id)).ToArray();
I'm assuming you're running a SQL server somewhere from which the data comes from?
The query will remain in LINQ to SQL space until, as suggested in both previous answers, you call ToList() or ToArray() or similar. Only then does it actually become LINQ to Object, and is the data instantiated and will it persist in your program's memory space.
In this way, after your changes to your list, you can execute the same query again and get a different list which does not have your changes in it.
Maybe a better explanation is the following:
(from id in ids select facade.GetObjectById(id)).Where(o=>string.IsNullOrEmpty(o.Name))
This will give an error because there is no translation from string.IsNullOrEmpty to SQL.
(from id in ids select facade.GetObjectById(id)).ToList().Where(o=>string.IsNullOrEmpty(o.Name))
This will not give an error, because after the ToList() you are in object space.

Resources