Nelson T Joseph
Nelson T Joseph

Reputation: 2763

Read from Windows Phone Local database

I have a database in my windows phone app. From a Table I need to select some rows. I need to apply more than one Where conditions in my query. How can I write such a query?

My table contains Username, DocumentId, FileType, FileLocation, FileSize etc..

I need to select rows using Username, DocumentId and FileType. How to write the query for that?

Now I am using the following code :

using (DocumentDataContext DocDb = new DocumentDataContext(strConnectionString))
{
    IQueryable<Document> DocQuery = from Doc in DocDb.Documents where Doc.UserName == txtName.Text select Doc;
    Document Docs= DocQuery.FirstOrDefault();
}

I can select one document using this query. I want to select all rows satisfy the conditions. Can I do this in a single query?

Upvotes: 0

Views: 687

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

Of course you can. You can extend your where condition using standard or/and keywords:

from Doc in DocDb.Documents
where Doc.UserName == txtName.Text && dic.FileType == "some type" && Doc.UserName == "some name"
select Doc;

To get more than one row use ToList instead of FirstOrDefault. It returns a list of elements containing the query results:

var Docs= DocQuery.ToList();

Upvotes: 2

Nelson T Joseph
Nelson T Joseph

Reputation: 2763

I found the solution.

To select more than one document and to apply more than one condition in "WHERE" clause, I made some changes in the code as follows

IList<Document> DocumentList = null;

using (DocumentDataContext DocDb = new DocumentDataContext(strConnectionString))
{
    IQueryable<Document> DocQuery = from Doc in DocDb.Documents where Doc.UserName == txtName.Text & Doc.DocumentId == docId & Doc.FileType == fileType select Doc;
    DocumentList = DocQuery.ToList();
}

In the query I used "AND" first to add more condition in "WHERE" clause. When I changed "AND" to "&" it works properly, and use an IList<> for getting more than one record

Upvotes: 0

Related Questions