Examine query generated from MongoDB projection by C# driver - mongodb-csharp-2.0

I'm querying data with C# driver for mongodb using projection. The code is like:
var filter = Builders<User>.Filter.Eq("_id", userId);
var projection = Builders<User>.Projection.Expression(u => Tuple.Create(u.UserId, u.UserName));
var cursor = await collection.Find(filter).Project(projection).ToCursorAsync();
How do I check the query generated by the C# code?

Related

Create an exact copy of Analysis Services database with AMO

I'm trying to automate the creation of an exact copy of an Analysis Services database using Microsoft.AnalysisServices namespace. My code is:
using (var server = new Server())
{
server.Connect(connString);
var newDb = server.Databases.GetByName(dbName).Clone();
newDb.Name = newDbName;
newDb.ID = server.Databases.GetNewID();
server.Databases.Add(newDb);
newDb.Update(UpdateOptions.ExpandFull);
server.Disconnect();
}
However it seems it creates an empty database instead because from SSMS i'm not able to see any tables, datasources and similar (check my screenshot here).
Is there a way to fix this? Thanks.
#gmarchi
You need to clone the child Model object using Model.clone() method as well to make to make it work. You can add Model using code like below -
var connString = "Provider=MSOLAP;Data Source=asazure://westus2.asazure.windows.net/xxxxxxxxxxxx:rw";
var dbName = "AW Internet Sales";
var newDbName = "CloneDb";
using (var server = new Server())
{
server.Connect(connString);
var newDb = server.Databases.GetByName(dbName).Clone();
var newModel = server.Databases.GetByName(dbName).Model.Clone();
newDb.Name = newDbName;
newDb.ID = server.Databases.GetNewID();
newDb.Model = newModel;
server.Databases.Add(newDb);
newDb.Update(UpdateOptions.ExpandFull);
server.Disconnect();
}
Please find below the screenshot after the code run.
Please let me know if you have any questions.
Thanks.

how to read Data context parameter from a crm to silverlight webresource

i have a silverlight webresource in my crm 2011.
now my task is to append a data parameter in url like
https://www.............../data=1234
now in silverlight application i need to read this data parameter.
i have tried to read the data
if (App.Current.Host.InitParams["data"] != string.Empty)
{
string CallerNumber = App.Current.Host.InitParams["data"];
...
and
i tried filtering the code to like
string url = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
var uri = new Uri(url);
var CallerNumber = uri.Query;
callernumber will have the ?data=1234
but still i am not able to get the data parameter. Do help
I believe that you will find those in the query string. You can access it like this
foreach (var item in HtmlPage.Document.QueryString)
{
//Do something like the data
}

How to get a single value from db using Fluent Hibernate

I'm new to Fluent Hibernate And I'm stuck with a problem I want to get the email id of a user by using his user name means I want to implement the following code using fluent Hibernate
Select emailId from Table where username="User"
I tried the following code but its not give me what i want
public string ForgetPassword(string user)
{
var factory = CreateSessionFactory();
using (var session = factory.OpenSession())
{
var getEmail = session.Query<ClsAccountBL>()
Select(u => u.Email).Where(u => u.User == user).ToString();
return getMail;
}
}
Please help me to solve this
Instead of .ToString() use FirstOrDefault(). The LINQ does not return "single" element.

Join using Linq failing with Entity Framework 4.0

I'm having trouble running a join linq query against EF 4.0. Below is the code, stripped of all unnecessary statements, still reproducing the error)
using (var threadRepo = new Repo<Thread>())
using (var postRepo = new Repo<Post>())
{
var query = threadRepo
.Join(postRepo, t => t.PostId, s => s.Id, (t, s) => 1);
var array = query.ToArray();
}
Repo is my implementation of the repository pattern, and the join method looks like this:
public IEnumerable<TResult> Join<TInner, TKey, TResult>(
IEnumerable<TInner> inner,
Expression<Func<TEntity, TKey>> outerSelector,
Expression<Func<TInner, TKey>> innerSelector,
Expression<Func<TEntity, TInner, TResult>> result)
{
return _objectSet.Join(inner, outerSelector, innerSelector, result);
}
The error I get is
Unable to create a constant value of type 'Post'.
Only primitive types ('such as Int32, String, and Guid')
are supported in this context.
The same query works in LinqPad against the same database (though offcourse, there is not EF 4.0 there)
from t in Thread
join p in Post on t.PostId equals p.Id
select 1
Any clues on why Linq is giving me this exception?
Update
Based on a suggestion below, I tried using a common datacontext for both the repositories using a unit of work. However that doesn't seem to fix the issue. Below is the code I used
using (var uow = new UnitOfWork<CommunicationEntities>())
{
using (var threadRepo = new Repo<Thread>(uow))
using (var postRepo = new Repo<Post>(uow))
{
var query = threadRepo
.Join(postRepo, t => t.PostId, s => s.Id, (t, s) => 1);
var array = query.ToArray();
}
}
This gives me the same error as before.
Thanks
Jaspreet
A common mistake I have seen is that the data context is on the repository level and each repository uses a different data context - in this case that would explain the error. Instead your repositories all should share the same data context using the Unit of Work pattern.

Accessing Facebook C# SDK result Object using .NET 3.5 API?

Consider the following in .NET 3.5 (using the Bin\Net35\Facebook*.dll assemblies):
using Facebook;
var app = new FacebookApp();
var result = app.Get("me");
// want to access result properties with no dynamic
... in the absence of the C# 4.0 dynamic keyword this provides only generic object members.
How best should I access the facebook properties of this result object?
Are there helper or utility methods or stronger types in the facebook C# SDK, or should I use standard .NET reflection techniques?
This code sample shows 3.5 usage, without needing the C# dynamic keyword:
// Using IDictionary<string, object> (.Net 3.5)
var client = new FacebookClient();
var me = (IDictionary<string,object>)client.Get("me");
string firstName = (string)me["first_name"];
string lastName = (string)me["last_name"];
string email = (string)me["email"];
var accesstoken = Session["AccessToken"].ToString();
var client = new FacebookClient(accesstoken);
dynamic result = client.Get("me", new { fields = "name,id,email" });
Details details = new Details();
details.Id = result.id;
details.Name = result.name;
details.Email = result.email;
You can also create a facade object around the IDictionary, as explained here.