How can you get a single column back from a query instead of a whole object?
I could do something like this to get the whole object, but all I want is the names:
IList<Tribble> tribbles = session.CreateCriteria(typeof(Tribble)).List<Tribble>();
IList<string> names = new List<string>();
foreach (Tribble t in tribbles) {
names.Add(t.Name);
}
I would like to be able to specify additional criteria, so is it possible to just exclude certain columns from being retrieved?
You could use the LINQ select clause and reference the property that relates to your Name column. Show activity on this post. If you're fetching a single item only then, you need use select before your FirstOrDefault()/SingleOrDefault(). And you can use anonymous object of the required properties.
In Entity Framework Core we can select specific columns by using either anonymous types or DTOs.
var Q1 = (ds. Tables[1]. AsEnumerable() .
Here is the solution I eventually ended up using:
ICriteria c = session.CreateCriteria(typeof(Tribble));
c.SetProjection(Projections.ProjectionList().Add(Projections.Property("Name")));
IList<string> names = c.List<string>();
I got this idea from this old StackOverflow question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With