In most cases, classes are known beforehand (e.g. Customer, Order); they're described for ORMs (e.g. Entity Framework, LINQ to SQL, NHibernate, BLToolkit) using visual designers, attributes in code or configuration files. When you need to use objects of class Customer, for example, you can write strongly-typed queries like this:
db.Customers
.Where(c => c.FirstName == "John")
.Select(c => new { c.Id, c.LastName })
.GroupBy(c => c.Id)
However, in my application, input data will be processed into models which are defined by users at runtime. Users will be able to add and remove properties, therefore I cannot define models in the code. The idea that this will require manually generating string SQL queries horrifies me. I'd like to be able to write code like this:
db.Tables["Customers"]
.Where("c.FirstName = :FirstName")
.Select(new[] { "c.Id", "c.LastName" })
.GroupBy("c.Id")
.Param(":FirstName", "John")
(Just example invented syntax.)
Question: is there a library for .NET which helps to construct complex SQL queries without defining models in the code first?
P.S. Looks like libraries like this exist, just not for .NET. I'd love to see something like SQLAlchemy, Ruby on Rails ActiveRecord, PHP Yii ActiveRecord, Django Models etc.
Solution to the problem: Simple.Data by Mark Rendle. It allows to write code like this without defining any classes or properties:
Database.Open().Users.FindAllByEmail(email).FirstOrDefault()
(It was mentioned in the description of the Rob Conery's massive library.)