NHibernate QueryOver Sum within JoinQueryOver - nhibernate

Though I was reading through the NHibernate Cookbook and all available forum-posts up and down, I'm still not able to get this simple query done:
I have users with everyone having one account. Each account hast a balance.
The classes look like that:
public class User
{
public virtual int Id { get; set; }
public virtual Account Account { get; set; }
public virtual bool Active { get; set; }
}
public class Account
{
public virtual int Id { get; set; }
public virtual double Balance { get; set; }
}
Now I would like to sum the balance of all active users. Nothing more...
In plain SQL it is quite easy:
SELECT SUM(a.Balance)
FROM User u
INNER JOIN Account a
ON u.Account_id = a.Id
WHERE u.Active = 'true'
I don't have any I idea, how I could solve that with the new QueryOver-Api from NHibernate 3. Could you please provide a code-example?
Thank you in advance!
Daniel Lang
EDIT
I know, that with NHibernate Linq it is very easy too, but I would like to solve it using QueryOver... Here is the working Linq-Example:
var result = Session.Query<User>()
.Where(x => x.Active)
.Sum(x => x.Account.Balance)
SOLUTION
Thanks to AlexCuse I could find the final solution (he was very very close) - here is the full code:
User userAlias = null;
Account accountAlias = null;
session.QueryOver<User>(() => userAlias)
.JoinAlias(() => userAlias.Account, () => accountAlias)
.Where(() => userAlias.Active)
.Select(Projections.Sum<Account>(acct => accountAlias.Balance))
.SingleOrDefault<double>()

Have you tried something like this?
session.QueryOver<User>(() => userAlias)
.JoinAlias(() => userAlias.Account, () => accountAlias)
.Where(() => userAlias.Active)
.Select(Projections.Sum<Account>(acct => acct.Balance))
.UnderlyingCriteria.UniqueResult()
I'm not sure what the UniqueResult equivalent is in the QueryOver API, so had to go through the underlying criteria.

You wrote in your answer:
User userAlias = null;
Account accountAlias = null;
session.QueryOver<User>(() => userAlias)
.JoinAlias(() => userAlias.Account, () => accountAlias)
.Where(() => userAlias.Active)
.Select(Projections.Sum<Account>(acct => accountAlias.Balance))
.SingleOrDefault<double>()
You don't need to have an alias for the generic type. It could be:
Account accountAlias = null;
session.QueryOver<User>()
.JoinAlias(user => user.Account, () => accountAlias)
.Where(user => user.Active)
.Select(Projections.Sum<Account>(acct => accountAlias.Balance))
.SingleOrDefault<double>()

Related

Joining 3 tables and using a left outer join with linq in EF Core 3.1.1

I have 3 tables, Notices, Users, and Likes. I want to get all notices with user name and information if user likes this notice.
So far I have this code, but it returns one notice multiple times (one for each like):
return context.notices
.GroupJoin(context.Users, notice => notice.CreatedBy, user => user.Id, (notice, users) => new { notice, users })
.SelectMany(group => group.users.DefaultIfEmpty(), (group, user) =>
new
{
group.notice,
user
})
.GroupJoin(context.Likes, noticeDto => noticeDto.notice.Id, like => like.ItemId, (noticeDto, likes) => new { noticeDto, likes })
.SelectMany(group => group.likes.DefaultIfEmpty(), (group, like) =>
new NoticeDto
{
CreatedByName = (group.noticeDto.user == null ? "" : group.noticeDto.user.FullName),
Id = group.noticeDto.notice.Id,
Liked = like.CreatedBy == userId,
});
I also tried this code.. but I am getting an error:
return context.notices
.GroupJoin(context.Users, notice => notice.CreatedBy, user => user.Id, (notice, users) => new { notice, users })
.SelectMany(group => group.users.DefaultIfEmpty(), (group, user) =>
new
{
group.notice,
user
})
.GroupJoin(context.Likes, noticeDto => noticeDto.notice.Id, like => like.ItemId, (noticeDto, likes) => new { noticeDto, likes })
.Select((group) =>
new NoticeDto
{
CreatedByName = (group.noticeDto.user == null ? "" : group.noticeDto.user.FullName),
Id = group.noticeDto.notice.Id,
Liked = group.likes != null ? group.likes.Any(w => w.CreatedBy == userId) : false,
});
This is the error I get:
Processing of the LINQ expression 'DbSet
.LeftJoin(
outer: DbSet
.AsQueryable(),
inner: notice => notice.CreatedBy,
outerKeySelector: user => user.Id,
innerKeySelector: (notice, user) => new {
notice = notice,
user = user
})
.GroupJoin(
outer: DbSet,
inner: noticeDto => noticeDto.notice.Id,
outerKeySelector: like => like.ItemId,
innerKeySelector: (noticeDto, likes) => new {
noticeDto = noticeDto,
likes = likes
})'
by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core.
Can anyone help me achieve what I need?.. Thank you.
Notice
public Guid Id { get; set; }
public Guid CreatedBy { get; set; }
User
public Guid Id { get; set; }
public string FullName{ get; set; }
Like
public Guid Id { get; set; }
public Guid CreatedBy { get; set; }
public Guid ItemId { get; set; }
Like's ItemId is Notice's Id? Notice's and Like's CreatedBy is User's Id?
If so, try this.
return context.notices.Include(x => x.Users)
.Include(x => x.Likes)
.Include("Likes.Users");
Database
Result
EDITED
As you do not have foreign key and relationship, then you can try this
var users = context.Users;
return context.notices.Select(x => new Notice()
{
Id = x.Id,
CreatedBy = x.CreatedBy,
Users = users.Where(y => y.Id == x.CreatedBy).FirstOrDefault(),
Likes = context.Likes.Where(y => y.ItemId == x.Id)
.Select(y => new Likes()
{
Id = y.Id,
CreatedBy = y.CreatedBy,
ItemId = y.ItemId,
Users = users.Where(z => z.Id == y.CreatedBy).FirstOrDefault()
}).ToList()
});

Fluent NHibernate - NHibernate.QueryException: could not resolve property

To start, yes, there are tons of similar questions here on Stack Overflow, but I've browsed them all and the problems that plague most of them look correct on my issue.
Basically, I'm trying to access an Object's Object via my query, and that could be the problem: is that not allowed? I can access the Id field, always, but any other member variable cannot be accessed.
Here are my Objects:
public class Logfiles
{
public virtual int Id { get; set; }
public virtual bool IsActive { get; set; }
public virtual DateTime LastReference { get; set; }
}
public class LogMerges
{
public virtual int Id { get; set; }
public virtual DateTime CreationDate { get; set; }
public virtual Logfiles Logfile { get; set; }
}
And here are my mappings:
class LogfilesMap : ClassMap<Logfiles>
{
public LogfilesMap()
{
Not.LazyLoad();
Table("logfiles");
Id(x => x.Id, "id").Not.Nullable().Unique().GeneratedBy.Identity().UnsavedValue(0);
Map(x => x.IsActive, "is_active").Not.Nullable().Default("true");
Map(x => x.LastReference, "last_reference").Not.Nullable();
}
}
class LogMergesMap : ClassMap<LogMerges>
{
public LogMergesMap()
{
Not.LazyLoad();
Table("logmerges");
Id(x => x.Id, "id").Not.Nullable().Unique().GeneratedBy.Identity().UnsavedValue(0);
Map(x => x.CreationDate, "creation_date").Not.Nullable();
References(x => x.Logfile, "logfile_id");
}
}
My table and columns have the names:
logfiles - id, is_active, last_reference
logmerges - id, creation_date, logfile_id
My code that I'm using to query:
var query = session.QueryOver<LogMerges>()
.Where(log => log.Logfile.IsActive == true);
IEnumerable<LogMerges> logmerges = query.List().OrderBy(c => c.CreationDate);
Performing the Query causes the error and generates:
NHibernate.QueryException: could not resolve property: Logfile.IsActive of: LogMerges
The only thing I can guess is that I'm not allowed to do the "log.Logfile.IsActive" chain of objects in these queries. It compiles just fine and I can't see why I wouldn't be able to do this. If I change it to:
var query = session.QueryOver<LogMerges>()
.Where(log => log.Logfile.Id == 0);
...the query goes through. However, if I try to access the other member variable:
var query = session.QueryOver<LogMerges>()
.Where(log => log.Logfile.LastReference == DateTime.Now);
...I get a similar "could not resolve property" error message.
So if it turns out that I CANNOT do the Object chain of "log.Logfile.IsActive", what's the proper way to implement it? Do I need to perform a bunch of JOINs to do this?
Thanks in advance for any help!
Yes, you do need do need to join on Logfile. Nhibernate is ultimately going to turn your QueryOver query into SQL, so accessing a property on a referenced table doesn't make sense. In other words, you couldn't write the SQL:
select
*
from
logmerges
where
logmerges.logfile.last_reference = ...
Obviously you'd get a syntax error here--you'd need to join to LogFile:
select
*
from
logmerges
inner join logfiles on logfiles.id = logmerges.logfile_id
where
logfiles.last_reference = ...
And so therefore you can't write QueryOver like that either.
var query = session.QueryOver<LogMerges>()
.JoinQueryOver(lm => lm.LogFile)
.Where(lf => lf.LastReference == DateTime.Now)
.List<LogMerges>();

RavenDb static Index: query on child collection objects

I have a document structure like the following:
Employer => Positions => RequiredSkills
Employer has a collection of Position
Positions have a collection of RequiredSkill.
Required Skill consists of a skill (string) and a proficiency (enum).
If I use a dynamic index it seems to return company fine, however I want to use an index to populate MVC view models to return to the UI.
I'm really new to Raven so my apologies for doing anything stupid/unnecessary!
I've got the following mapping:
public class PositionSearch : AbstractIndexCreationTask<Employer>
{
public PositionSearch()
{
Map = employers =>
from employer in employers
from position in employer.Positions
select new
{
EmployerId = employer.Id,
EmployerName = employer.Name,
PositionId = position.Id,
PositionTitle = position.Title,
position.Location,
position.Description,
RequiredSkills = position.RequiredSkills
};
StoreAllFields(FieldStorage.Yes);
Index("RequiredSkills_Skill", FieldIndexing.Analyzed);
}
}
However when I try to execute the following query:
var results = session.Query<PositionSearchResultModel, PositionSearch>()
.Customize(x => x.WaitForNonStaleResults())
.Where(x=>x.RequiredSkills.Any(y=>y.Skill == "SkillName"))
.ProjectFromIndexFieldsInto<PositionSearchResultModel>()
.ToList();
I get the following error:
System.ArgumentException:
The field 'RequiredSkills_Skill' is not indexed,
cannot query on fields that are not indexed
Can anyone see what I'm doing wrong or suggest another approach for me please?
Thanks,
James
UPDATE my view model - Thanks :
public class PositionSearchResultModel
{
public PositionSearchResultModel()
{
RequiredSkills = new HashSet<SkillProficiency>();
}
public string EmployerId { get; set; }
public string EmployerName { get; set; }
public string PositionId { get; set; }
public string PositionTitle { get; set; }
public string Location { get; set; }
public string Description { get; set; }
public ICollection<SkillProficiency> RequiredSkills { get; set; }
}
Because you want to do an analyzed search against the skill name, you need to isolate it as a separate index entry.
public class PositionSearch
: AbstractIndexCreationTask<Employer, PositionSearchResultModel>
{
public PositionSearch()
{
Map = employers =>
from employer in employers
from position in employer.Positions
select new
{
EmployerId = employer.Id,
EmployerName = employer.Name,
PositionId = position.Id,
PositionTitle = position.Title,
position.Location,
position.Description,
position.RequiredSkills,
// Isolate the search property into it's own value
SkillsSearch = position.RequiredSkills.Select(x => x.Skill)
};
// you could store all fields if you wanted, but the search field
// doesn't need to be stored so that would be wasteful.
Store(x => x.PositionId, FieldStorage.Yes);
Store(x => x.PositionTitle, FieldStorage.Yes);
Store(x => x.Location, FieldStorage.Yes);
Store(x => x.Description, FieldStorage.Yes);
Store(x => x.RequiredSkills, FieldStorage.Yes);
// Any field you are going to use .Search() on should be analyzed.
Index(x => x.SkillsSearch, FieldIndexing.Analyzed);
}
}
Note that I specified the projection as the result of the index. This is syntactic sugar. It's not wrong to leave it off, but then you have to specify your search field using a string.
You'll also need to add the search field to your results class
public string[] SkillsSearch { get; set; }
It really doesn't matter what type it is. A string array or collection will do just fine. You could also use just a string or an object, because it's only the name that's relevant.
When you query against this index, use the .Search() method, like this:
var results = session.Query<PositionSearchResultModel, PositionSearch>()
.Customize(x => x.WaitForNonStaleResults()) // only use this in testing
.Search(x=> x.SkillsSearch, "SkillName")
.ProjectFromIndexFieldsInto<PositionSearchResultModel>() // AsProjection would also work
.ToList();
Note that the only reason you have to store so many fields is because you want to project them. If you separated the positions into their own documents, you would have much smaller indexes and much less to project. Keep in mind that when you project, all of the fields in the original document are already there and come directly from the document store, rather than having to be copied into the index. So if your original documents more closely match your desired results, then there's less work to do.

Fluent Nhibernate - how do i specify table schemas when auto generating tables in SQL CE 4

I am using SQL CE as a database for running local and CI integration tests (normally our site runs on normal SQL server). We are using Fluent Nhibernate for our mapping and having it create our schema from our Mapclasses. There are only two classes with a one to many relationship between them. In our real database we use a non dbo schema. The code would not work with this real database at first until i added schema names to the Table() methods. However doing this broke the unit tests with the error...
System.Data.SqlServerCe.SqlCeException : There was an error parsing the query. [ Token line number = 1,Token line offset = 26,Token in error = User ]
These are the classes and associatad MapClasses (simplified of course)
public class AffiliateApplicationRecord
{
public virtual int Id { get; private set; }
public virtual string CompanyName { get; set; }
public virtual UserRecord KeyContact { get; private set; }
public AffiliateApplicationRecord()
{
DateReceived = DateTime.Now;
}
public virtual void AddKeyContact(UserRecord keyContactUser)
{
keyContactUser.Affilates.Add(this);
KeyContact = keyContactUser;
}
}
public class AffiliateApplicationRecordMap : ClassMap<AffiliateApplicationRecord>
{
public AffiliateApplicationRecordMap()
{
Schema("myschema");
Table("Partner");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.CompanyName, "Name");
References(x => x.KeyContact)
.Cascade.All()
.LazyLoad(Laziness.False)
.Column("UserID");
}
}
public class UserRecord
{
public UserRecord()
{
Affilates = new List<AffiliateApplicationRecord>();
}
public virtual int Id { get; private set; }
public virtual string Forename { get; set; }
public virtual IList<AffiliateApplicationRecord> Affilates { get; set; }
}
public class UserRecordMap : ClassMap<UserRecord>
{
public UserRecordMap()
{
Schema("myschema");
Table("[User]");//Square brackets required as user is a reserved word
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Forename);
HasMany(x => x.Affilates);
}
}
And here is the fluent configuraton i am using ....
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(
MsSqlCeConfiguration.Standard
.Dialect<MsSqlCe40Dialect>()
.ConnectionString(ConnectionString)
.DefaultSchema("myschema"))
.Mappings(m => m.FluentMappings.AddFromAssembly(typeof(AffiliateApplicationRecord).Assembly))
.ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
.ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close")) //This is included to deal with a SQLCE issue http://stackoverflow.com/questions/2361730/assertionfailure-null-identifier-fluentnh-sqlserverce
.BuildSessionFactory();
}
The documentation on this aspect of fluent is pretty weak so any help would be appreciated
As usual, 10 minutes after posting i answer my own questions. The trick was to not declare the schema in the the ClassMaps. Instead i used the DefaultSchema method in the fluent configuration. So my actual 'live' configuration looks like this :
var configuration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("connectionStringKey"))
.DefaultSchema("myschema"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<AffiliateApplicationRecord>());
return configuration;
And my integration tests look like this:
return Fluently.Configure()
.Database(
MsSqlCeConfiguration.Standard
.Dialect<MsSqlCe40Dialect>()
.ConnectionString(ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssembly(typeof(AffiliateApplicationRecord).Assembly))
.ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
.ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close")) //This is included to deal with a SQLCE issue http://stackoverflow.com/questions/2361730/assertionfailure-null-identifier-fluentnh-sqlserverce
.BuildSessionFactory();
Hopefully someone else will get somevalue out of this...
I think this has more to do with SQL Server CE than nhibernate. I am pretty sure that Sql Server CE will not accept schemas at all. its not a supported feature.
See the Create Table Documentation on MSDN

nhibernate inner join a table without a property in class

i have the following model:
public class FlatMap : ClassMap<Flat>
{
public FlatMap()
{
Id(m => m.FlatID).GeneratedBy.Identity();
Map(m => m.Name);
Map(m => m.Notes);
Map(m => m.Released);
}
}
public class BuildingMap : ClassMap<Building>
{
public BuildingMap()
{
Id(i => i.BuildingID).GeneratedBy.Identity();
Map(m => m.Name);
HasMany<Flat>(m => m.Flats).Cascade.All().KeyColumn("BuildingID").Not.LazyLoad();
}
}
public class ContractMap : ClassMap<Contract>
{
public ContractMap()
{
Id(m => m.ContractID).GeneratedBy.Identity();
Map(m => m.Amount);
Map(m => m.BeginIn);
Map(m => m.EndIn);
References(m => m.RentedFlat);
}
}
how can i make the following query using fluent nhibernate ?
Select * From Contract
Inner Join Flat On Contract.RentedFlatID = Flat.ID
Inner Join Building On Building.BuildingID = Flat.BuildingID
Where Building.BuildingID = #p0
especially there no reference from Flat to Building?? and i don't want it to be !
of course the reference i am talking about in order to be able to do something like this
var criteria = session.CreateCriteria<Contract>().CreateCriteria ("RentedFlat").CreateCriteria ("Building"/*there is no such property in Flat class*/);
i solved a problem, but not the way as i think is good.
but i will make this as an answer until someone provide me a better solution.
i add a property BuildingID to the Flat class, and modified the mapping class to :
Map(m => m.BuildingID);
now i can do the following query:
criteria.CreateCriteria("RentedFlat")
.Add(Restrictions.Eq("BuildingID", selectedBuilding.BuildingID));