I'm trying to search for some values in my Azure SQL Database. As i understand, the data returns in a object of type List, but i dont know how to read this query and pass the values to variables.
This is my code:
List<TodoItem> items = await todoTable
.Where(todoItem => todoItem.Text != null)
.ToListAsync();
MessageBox.Show(items.ToString());
This way, it will run a query for data that contains the "Text" field different then null.
But how can i read this list with the query data to store these values?
Running this code, i got the following:
Microsoft.WindowsAzure.MobileServices.TotalCountList'1[Project.TodoItem]
So, how is the easiest way to read this values, and store them into variables so i can work with this data?
Thanks a lot, friends.
You are calling .ToString() on your list. This won't give the results you expect.
Try the following, which should tell you haw many results you get back from your query.
MessageBox.Show(items.Count.ToString());
You should find this matches the number of records, then you can loop through the list contents to get at the results you want.
Related
I am rather new to c# and MongoDb in particular. I have a method GetAllTickets() that returns a List<Ticket> with all entries of a MongoDb collection called "tickets". I want to be able to only get the entries with a certain filter from the collection in a different method called GetTicketsWithStatus(Status status) (where Status is an enum). How can I do this?
I have tried List<Ticket> tickets = collectionOfTickets.Find(x => x.ticketStatus == status).ToList<Ticket>(); (where collectionOfTickets is an IMongoCollection<Ticket> but that doesnt get any objects in the List. Any help will be appreciated.
P.S.: GetAllTickets() uses List<Ticket> tickets = collectionOfTickets.AsQueryable().ToList<Ticket>(); and it fills in the List properly. It's only when I try to use a filter that everything breaks and the list is not filled in.
I managed to solve this, if someone else has this problem in the future.
First of all, my Ticket model had enums in them, which at first I had (wrongly) saved as strings in mongoDb, then saved those strings in the model, then used methods to transform those strings into enums. I just turned all strings in mongodb into ints and saved the numerical position of the enum in the mongodb doc. Much easier to translate into enum, and makes the Linq expression above work, so it got solved.
Second of all, I tried using Builders to create a filter variable, but for some reason my Find() method would not accept that filter.
Overall, the problem lied in the way I had stored enum values in mongoDb, and how i was translating them on my model
I have records that have a string property called Project. The values these normally have are like A-A-40019-0 but in reality they could be anything.
I need to be able to extract the numeric values from the Project project property so that I can then try and cast it to a ulong so that it can be sorted by.
I'm trying the following code to select the number values from the Project property.
return jobs.Select(x => new JobViewModel
{
Sequence = x.Project.Where(y => char.IsDigit(y)).ToString()
});
When I try this I get the following error
DbExpressionBinding requires an input expression with a collection
ResultType.
I need to use Linq to Entities as I can't afford to load all records into memory.
I'm using SQL Server.
You don't say which DB you are using but I did find this for SQL Server. https://www.mytecbits.com/microsoft/sql-server/extract-numbers-from-string#:~:text=%20Extract%20Numbers%20From%20String%20In%20SQL%20Server,you%20want%20to%20split%20this%20delimited...%20More%20
Hopefully similar technique can be used for the DB you are using.
You could make a view with a column calling that function added on the end and then you can sort it.
or purely in C# which I haven't tried:
Convert.ToInt32(new string(y.project.Where(c => Char.IsDigit(c)).ToArray())
I save my data in a binary-look string, "100010" ,for example. And I want to check whether it has same value in corresponding place with the other string "100000".
So I try to use "Intersection". In this Condition, the result of intersection will be "100000", and it could be seen as the item I need for my requirement. But how can I use this conception when I query a Entity to Linq statement?
Here is my thought:
var chemicals = db.ChemicalItem.Where(c => c.CategoryNumber.ToCharArray().Intersect(catekey.ToCharArray()).Count()>0);
"CategoryNumber" is my data, and "catekey" is the string for comparing. Both of them are binary-look string(cantain 6 chars). And if the count is not 0,they have '1's in the same index. And I can get the correct query.
Sadly, It didn't work. I always get DbExpressionBinding Error. Can somone tell me What's Wrong? Thanks.
PS:I'm not good at English and post the question here first time, sorry for my bad expression and thank for your reading.
LINQ to Entities is trying to create a SQL query out of your condition, but is not able to do it for the expression you specified.
One way to "fix" the problem would be to do the filtering in code instead of in SQL, but this will impact performance, because all of the records will be retrieved to the client and filtered there. This is how you could do it (notice the added ToList()):
var chemicals = db.ChemicalItem.ToList().Where(c => c.CategoryNumber.ToCharArray().Intersect(catekey.ToCharArray()).Count()>0);
A suggested way would be to do the filtering in SQL, but in this case you will need to write an equivalent stored procedure in SQL which will do the filtering and call that from your EF code. Still such filtering will not be very effective because SQL will not be able to use any indices and will always need to do a table scan.
i followed this tutorial for setting up an upload/download to/from sql database.
http://dotnetawesome.blogspot.co.uk/2013/11/how-to-upload-and-download-files-tofrom.html
It works just fine. But i want to modify the populate method so that it will only populate files where the fileid exists within a list that i've stored in session state.
The problem is i've looked around and i can't make any sense of the lambda expressions or work out how to do this.
Basically, i keep a list of the fileIDs in the session, (which is renewed on first page load) so it will only show the files uploaded for that form submission. (it's a claim form)
using (Portal_Entities dc = new Portal_Entities()) {
List<WEBSITE_ATTACHMENTS> allFiles = dc.WEBSITE_ATTACHMENTS.ToList()
rptAttachments.DataSource = allFiles;
rptAttachments.DataBind();
}
I'm guessing i need to put a .Where or .Select on the .ToList here, but i'm not sure.
Like i need a sql type statement where field in ('value','value') where there values come from the list in the session.
Can anyone help?
You could try this one:
// Get the list of ints called fileIDs that you have stored in session.
List<int> ids = (List<int>)Session["fileIDs"];
// Declare an enumeration in which only the WEBSITE_ATTACHMENTS
// with an in contained in ids, will be contained.
List<WEBSITE_ATTACHMENTS> allFiles = dc.WEBSITE_ATTACHMENTS
.Where(x=>ids.Contains(x.fileId));
I am retrieving data from an Access database using CoolStorage, and can successfully populate a Team DataGridView by setting its DataSource as Team.List().
However, I want to use a LINQ query on the result set to return the number of users for each team. As this screenshot shows the result is being returned fine, however the DataGridView displays no data. If I switch the DataSource to be Team.List() it displays the teams without any problems (though obviously not the number of users).
Is there something I need to do in order to use the LINQ result as a DataSource? I can get round this by adding a property to my Team class, however I don't understand why I can't use the LINQ result.
You will need to materialize your datasource before it can be used.
Try changing
this.dgvTeams.DataSource = d;
to
this.dgvTeams.DataSource = d.ToList();
LINQ uses deferred execution. That is, you have built your LINQ query but it will not actually be evaluated to produce results unless you add a method or aggregate on the end of the query to force immediate evaulation, or you can enumerate the result.
Try using...
this.dgvTeams.DataSource = d.ToList();