Quick syntax question: I can return TFS items just fine using several other clauses, but I need to search by specific keyword. Query below:
WorkItemCollection results = wis.Query("SELECT [System.Id] FROM WorkItems WHERE [System.WorkItemType] = 'Dev Bug' AND [System.Keywords] = 'constellation'");
System.Keywords is the problem, this part of the clause is not correct. But I cannot find the right syntax anywhere. Help please!!
In future, if you run into a situation where you aren't sure about the name of the work item field. You can connect to the TFS database, open the team project collection database and look at the fields table.
Lets say for example [Tfs_DefaultCollection].[dbo].[Fields]. This will show you the field name, reference name, field Id, etc.
Related
Json string is updated to one of the columns of SQL Server, I want the rows that matches my condition and store it to a collection. In this case, I am applying the condition to the json key/ name in order to fetch the data. I achieved it in entity framework with the help of sqlquery function. But I trying to find other options that we have for same circumstance maybe linq or lambda.
Example it is a movie table that has 3 columns among which Movie_info column is a NVarChar that contains Json string.
id: 1
Storyline: "Blah Blah!!"
Movie_info [{"title":"The Shawshank Redemption","year":1994,"cast":["Morgan
Freeman", "Tim Robbins"]}]`
id: 2
Storyline: "free from corruption."
Movie_info [{"title":"Batman Begins","year":2005,"cast":[],"Director":
"Christopher Nolan"}]
.
..
...
I am trying to filter the movies that were released in the year 2005. Below is the code I tried and it worked successfully.
using (MoviesDbContext ctx = new MoviesDbContext())
{
var movieList = ctx.Movies.SqlQuery("SELECT * FROM [Movie].[dbo].[Movies]
WHERE JSON_VALUE(Movie_info, '$.year') = #value",
new SqlParameter("#value", 2005)).ToList();
}
With this code I got list of Movies that were released in the year 2005. How can I achieve the same using Lambda or using Linq queries because I don't want to hardcode SQL statements to the controller..
I am expecting the same result. suggestion on any other approach is much appreciated..
from visualstudiomagazine.com
There is, as yet, no support in Entity Framework or LINQ to leverage the TSQL JSON functionality.
You could use Reflectionsto get the Propertyname of the requested JSON item to put it in the Query, but be aware that reflections are slow. for further information see
https://www.tutorialspoint.com/csharp/csharp_reflection.htm
Is it possible to generate a raw SQL statement for insert/update operations using nHibernate without actually executing them? Assuming of course that everything (mappings, connectionStrings, etc.) is properly configured?
The closest thing I've found is to call:
Session.SessionFactory.GetClassMetadata(typeof(Client))
Which returns an object of type SingleTableEntityPersister containing SQLIdentityInsertString, that looks like this:
INSERT INTO Client (FirstName, LastName) values (?, ?)
But it would still require me to bind all of the properties manually, and on top of that SQLIdentityInsertString is a protected property. Are there any proper ways of doing that?
Okay, the closest thing I've found is to construct your own sql query with a string builder. First you need to extract your class metadata:
var metaData = Session.SessionFactory.GetClassMetadata(typeof(Client)) as SingleTableEntityPersister;
Then you can retrieve other information, such as:
var propertyNames = metaData.PropertyNames;
var tableName = metaData.TableName;
var firstPropertyValue = metaData.GetPropertyValue(client, propertyNames[0], EntityMode.Poco);
Once you have that information you can construct your own query manually. Not exactly the solution I wanted, but it's as close as it gets, I think.
However, one thing to note is that Session.CreateSQLQuery(string) method is currently bugged, and as a result SetParameter method doesn't work with more than 10 named parameters. There already seems to be a bug report created for this on NHbiernate's Jira.
I'm writing an application to retrieve work items from a given project collection. Currently I have seen the standard practice is along the lines of the following:
WorkItemStore workitemstore = tfs.GetService<WorkItemStore>();
string wiql = QueryToSelectWorkItems;
WorkItemCollection wic = workitemstore.Query(wiql);
Is there a way to retrieve the work items of a project without querying the database? E.g. A method that retrieves all work items of a team project collection.
First, the code above does not query the database directly. Querying the TFS DB's is unsupported. You are using WIQL (http://msdn.microsoft.com/en-us/library/bb130306.aspx#operators) to query the API.
If you make your query "Select ID From WorkItems" you will get all items in the collection. Add "where [System.TeamProject] = 'myprojname'" to filter by project.
If you want to query all work items across the team project collection, edit your work item query so that it doesn't have clauses to filter on the project, areas or iteration paths.
I am using Nucleon Database Master, which so far has been great for managing my mongodb servers. However, there are a few examples where they show the LINQ Query editor in use with your own data types and classes, but I can't find any documentation anywhere on how to do it.
There are hints in the comments in the c# query it generates:
//Linq to MongoDB Help:http://www.mongodb.org/display/DOCS/CSharp+Driver+LINQ+Tutorial
//The following sample shows a LINQ to MongoDB query. Use #FileInclude command to add your entity class for LINQ Query.
//var collection = this.db.GetCollection<Employee>("Employees");
//var result = from emp in collection.AsQueryable<Employee>()
//where emp.FirstName == "Andrew"
//select emp;
//Output(result);
Everything in the editor works great, apart from I have no idea how to "Use #FileInclude command to add your entity class LINQ Query".
I'm sure it's possible as they have screenshots demonstrating it on their website (I have emailed them, but haven't got a response).
http://www.nucleonsoftware.com/images/dm/LinqToEF_Select1.png
So, anyone either know how to do this, or otherwise have an idea what the #FileInclude thing might be about?
Have had a response back from Nucleon Software, and thought I'd share it here for anyone else with the same question:
You need to add a comment at the top of the query editor, e.g.:
//#FileInclude C:\myFile.cs
using System;
using System;
using System.Collections.Generic;
....
However, they apparently will soon be adding a feature to add you custom DLL's to the LINQ query.
I am using Lightswitch to build my application and I have the following problem.
In my database, I have three tables:
Article
Provider
ArticleProvider
Article and Provider have a many-to-many relation, therefore junction table ArticleProvider is needed.
Now, I want a screen in my application where the user can choose a provider and sees all articles which have a relation to this provider.
Using SQL, I would to it like this (123 is the Provider_Id I want to select).
SELECT *
FROM Article a
WHERE a.Id IN
(SELECT ap.Article_Id FROM ArticleProvider ap WHERE ap.Provider_Id=123)
In my Lightswitch application, I created a Query by clicking on the "Articles" Table in my Datasource and choosed "Add Query". I added a parameter ProviderId and switched to the source code editor to create my custom query:
partial void ArticleByProvider_PreprocessQuery(int? ProviderId,
ref IQueryable<Article> query)
{
...
}
Next I started to create my Linq Query. I think I need an IQueryable<ArticleProvider> Query to filter by them, so I tried:
(from art in query select art.ProviderQuery).AsQueryable<ArticleProvider>()
But, when trying this, I get a compile time error saying that this type can not be converted. So I tried this and it compiles fine:
(from art in query select art.ProviderQuery)
.AsQueryable<IDataServiceQueryable<ArticleProvider>>()
However, when using the returned IQueryable apList in my next query:
from ap in apList where ap.Provider.Id == 123 select ap.Article.Id
It seems that the fields Provider and Article can not be found. Also Visual Studio's code completion does not suggest these fields, only lots of methods and fields which are not in my database.
How can I solve this problem?
I played around with casts and other method calls like ToList(), but I get always stuck at this point. I am new to Linq and C#. Thank you in advance for any help.
EDIT:
I checked the return type of the first query by using:
var temp = (from art in query select art.ProviderQuery).AsQueryable()
The returned type is System.Linq.IQueryable<Microsoft.LightSwitch.IDataServiceQueryable<LightSwitchApplication.ArticleProvider>>
Your problem is that you are in the PreprocessQuery
This is for filtering data further, not adding extra.
If you look around a little this is mentioned a lot.
Give this query a try and see if this works:
partial void ArticleByProvider_PreprocessQuery(int? ProviderId,
ref IQueryable<Article> query)
{
query.Where(art => art.ArticleProviders
.Any(artProv => artProv.Provider.Id == ProviderId));
}
The idea is to get all Articles that at least matches the Provider Id.
Note: Haven't tested this code myself. But the idea should be there.