I want to query my documents in my DocumentDB database. I want to use LINQ to handle the DocumentDB query and want to query for facebookUsername field.
If I use the code below for querying the standard Id field, it works fine but when I try to do it using facebookUsername field, I get a compile error that reads
"'Microsoft.Azure.Documents.Document' does not contain a definition
for 'facebookUsername' and no extension method 'facebookUsername'
accepting a first argument of type
'Microsoft.Azure.Documents.Document' could be found (are you missing a
using directive or an assembly reference?)"
Here's the code I'm currently using for querying by Id and this works. I just want to be able to query the facebookUsername field.
dynamic doc = (from f in client.CreateDocumentQuery(collection.DocumentsLink)
where f.Id == myId.ToString()
select f).AsEnumerable().FirstOrDefault();
How do I modify my code to query by facebookUsername field?
var families = from f in client.CreateDocumentQuery<Family>(colSelfLink)
where f.Address.City != "NY"
select f;
will give you a List where Family: { "Address" : {"City": "NY"} } }
if you don't have an object like Family, in my case, then you can't use Linq to evaluate queries on dynamic objects. You need to then use the SQL Query Grammar.
var families = client.CreateDocumentQuery<Family>(colSelfLink. "SELECT * FROM c WHERE field=value").AsEnumnerable();
should work.
Related
Hello stackoverflowers,
I'm trying to use LINQ to Sharepoint for the first time, but my where keyword isn't recognized : "Could not find an implementation of the query pattern for source type 'Microsoft.SharePoint.SPList'. 'Where' not found".
Here is the request :
using System.Linq;
[...]
var query = from item in listToQuery
where item.Site == _siteToQuery
&& item.ReportType == _recordTypeToQuery
&& item.Date == stringDate
select item;
Result = listToQuery.GetItems(query);
listToQuery and Result are two SPListItemCollection.
Why is where not recognized ?
It's normal. The SharePoint Object doesn't implement Linq query, so that's why you have this exception.
To Query a SharePoint List you need to use a CAML Query (with an object of type SPQuery )
you can find a lot of documentation on internet about "how to query a sharepoint list programmatically"
But if you still want to user LINQ on Sharepoint, you can use SPMetal
I'm using Parse.com and I am running a query that obtains objects in a many-to-many relational table (call this table 'RelationTable'). Obviously this table has links to objects in another table (let's call this SubObject). Now, from this query, I need to filter results by searching on a field contained within the SubObject (call this SearchField).
Any ideas on how to do this? I already have the includeKey and am trying the '.' operator in SQL to access a field in the subclass, but it's not working. Below is the code I have so far:
ParseQuery<ParseObject> query = ParseQuery.getQuery("RelationTable);
query.include("subObject"); //subObject is field name where SubObject is stored. Note CAPS difference
query.whereContains("SubObject.SearchField", searchString);
You can create a subquery on the user object, and use whereMatchesQuery on your RelationTable query :
ParseQuery<ParseObject> query = ParseQuery.getQuery("RelationTable);
query.include("subObject");
ParseQuery<ParseObject> innerQuery = ParseQuery.getQuery("SubObject");
innerQuery.whereContains("SearchField", searchString);
query.whereMatchesQuery("subObject", innerQuery);
How can i convert linq to object query or any other Func delegate to string like sql statements
for example
var cat_list = new List<Cat> { ... };
var myquery = cat_list.Where(x => x.Age > 2 && x.Name.Contains("Kitty"));
Now myquery is IEnumerable<Cat>. how can i convert this to simply something like this
"Age > #p1 AND Name LIKE #p2"
how can i achieve this ??
Doing something like that is not simple. Have a look at the series of articles Building an IQueryable provider by Matt Warren. All the code he uses is available as a library too. That should help you get started.
You could write an expression tree parser and generate the sql. Your description contains a fault - myquery isn't IQueryable<Cat>, it is an IEnumerable<Cat>. As you tagged it correctly, this is linq-to-objects, not linq-to-sql. There is no information in the calls to construct a query.
Check out the method DataContext.GetCommand() which is passed an IQueryable object and returns the DbCommand object that corresponds to the query. The CommandText property of the DbCommand object shows the text of the query.
I am using Northwind Customers Table where I fill the dataset and get the datatable.
I am trying to use dynamic linq and want to select columnName dynamically
var qry = MyDataTable.AsEnumerable().AsQueryable().Select("new(Country)");
Right now I have hard coded country but even then I get this error
No property or field 'Country' exists in type 'datarow'
I would like to eventually change this query to take the column name dynamically.
Please help!!! thanks.
The important hint is here (in bold):
No property or field 'Country' exists
in type 'datarow'
The extension method AsEnumerable of the DataTable class returns an IEnumerable<T> where T has the type DataRow. Now the Select method of Dynamic LINQ wants to work with this type DataRow which hasn't a property Country of course.
You could try this instead:
var qry = MyDataTable.AsEnumerable().AsQueryable()
.Select("new(it[\"Country\"] as CountryAlias)");
it now represents a variable of type DataRow and you can use methods of this type and perhaps also the indexer in my example above. (Dynamic LINQ supports accessing array elements by an integer index, but I am not sure though if accessing an indexer with a string key will work.)
I've used Slauma's answer and it worked. In addition i was doing OrderBy with dynamic linq maybe this will help to someone. I'll just drop the code here.
string dynamicLinqText = $"it[\"{sortColumnName}\"] {sortDirection}"; //it["PERSON_NAME"] asc
result = result.AsEnumerable().OrderBy(dynamicLinqText).CopyToDataTable();
I am trying to figure out how to dynamically specify the properties for my select clause in a linq query.
Lets say I have a collection of employee objects. At run time, the end user will be specifying which properties they would like to see for those employees, so I need to be able to dynamically construct my Linq select clause.
I have used the dynamic Linq library, but I prefer not to use that, because it requires me to build a string to pass to the select method. I'd like to understand how to do this via Expressions.
This looks like something that fits more with your requirements of not using dynamic linq.
Use Reflection to get the dynamic Column Values
//columns variable has column name as comma separated String which you
can save in DB //example string columns ="Name,Id,Age";
var strColumns =columns.split(,);
foreach(var myObject in MyObjectcollection)
{
for(int index =0;index<strColumns.count();index++)
{
//Create a collection of objects
mycollection.add(myObject.GetType().GetProperty(strColumns[index]).GetValue(myObject, null));
}
}