Entity Framework getting data from the ENTIRE column - c#

I've got an api call getUser which returns ALL the info from the database at the specified column, but I want to exclude some things; phonenumber, email etc.
Tried fiddling with the LINQ, changeing the modelBuilder, changeing the optionBuilder
Have looked for any SQL saying "Select * from" but found nothing
Deleting things in my UserModel do work, however this is an ugly way of doing it.
Result<UserModel> result = new Result<UserModel>();
try
{
using (var db = new ComeatDBContext(optionsBuilder.Options))
{
var user = db.User
.Include(f => f.Nationality)
.Include(f => f.State)
.FirstOrDefault(e => e.Id == id && !e.IsDeleted);
result.ResponseObject = (new UserModel()).FromUser(user);
var house = db.House.FirstOrDefault(e => e.UserId == user.Id);
if (house != null)
{
result.ResponseObject.House = ( new HouseModel() ).FromHouse(house);
}
result.Success = true;
}
Expected specifying which data to include (or not include) but instead I get everything unless I set the values to "null" after they've been delievered by the DB.

The .FirstOrDefault(...) is what causes the underlying database query (SQL) to be executed. It will populate all properties that are defined on the entity. The data entity is then transformed to your UserModel object when you execute (new UserModel()).FromUser(user);.
If you want to reuse your UserModel class but not populate all the columns you can do something like this;
var userModel = db.User
.Select(x => new UserModel { Name = x.Name, Id = x.Id })
.FirstOrDefault(e => e.Id == id && !e.IsDeleted);
result.ResponseObject = userModel;

Related

EF Core Reuse subquery in different queries

I have a problem trying to reuse some subqueries. I have the following situation:
var rooms = dbContext.Rooms.Select(r => new
{
RoomId = r.Id,
Zones = r.Zones.Select(zr => zr.Zone),
Name = r.Name,
Levels = r.Levels.Select(lr => lr.Level),
IdealSetpoint = (double?)r.Group.Setpoints.First(sp => sp.ClimaticZoneId == dbContext.ClimaticZonesLogs.OrderByDescending(cz => cz.Timestamp).First().ClimaticZoneId).Setpoint??int.MinValue,
Devices = r.Devices.Select(rd => rd.Device)
}).ToList();
var tagsTypes = rooms.Select(r => r.Devices.Select(d => GetSetpointTagTypeId(d.DeviceTypeId))).ToList().SelectMany(x => x).Distinct().ToList();
predicate = predicate.And(pv => tagsTypes.Contains(pv.TagSettings.TagTypeId) &&
pv.ClimaticZoneId == dbContext.ClimaticZonesLogs.OrderByDescending(cz => cz.Timestamp).First().ClimaticZoneId);
var setpoints = valuesSubquery.Include(t=>t.TagSettings).Where(predicate).ToList();
This works fine, and generates the exact queries as wanted. The problem is that I want to have this subquery dbContext.ClimaticZonesLogs.OrderByDescending(cz => cz.Timestamp).First().ClimaticZoneId to be taken from a method and not repeat it every time I need it.
I've tested it with the database, where I have values in the corresponding tables, and I've tested the query with the database without any data in the corresponding tables. It works fine with no problems or exceptions.
But when I try to extract the repeating subquery in a separate method and execute it against empty database tables (no data) the .First() statement throws error. Here is the code:
protected long GetClimaticZoneId()
{
return dbContext.ClimaticZonesLogs.OrderByDescending(cz => cz.Timestamp).First().ClimaticZoneId;
}
and the query generation:
var rooms = dbContext.Rooms.Select(r => new
{
RoomId = r.Id,
Zones = r.Zones.Select(zr => zr.Zone),
Name = r.Name,
Levels = r.Levels.Select(lr => lr.Level),
IdealSetpoint = (double?)r.Group.Setpoints.First(sp => sp.ClimaticZoneId == GetClimaticZoneId()).Setpoint??int.MinValue,
Devices = r.Devices.Select(rd => rd.Device)
}).ToList();
var tagsTypes = rooms.Select(r => r.Devices.Select(d => GetSetpointTagTypeId(d.DeviceTypeId))).ToList().SelectMany(x => x).Distinct().ToList();
predicate = predicate.And(pv => tagsTypes.Contains(pv.TagSettings.TagTypeId) &&
pv.ClimaticZoneId == GetClimaticZoneId());
var setpoints = valuesSubquery.Include(t=>t.TagSettings).Where(predicate).ToList();
After execution I get InvalidOperationException "Sequence do not contain any elements" exception in the GetClimaticZoneId method:
I'm sure that I'm not doing something right.
Please help!
Regards,
Julian
As #Gert Arnold suggested, I used the GetClimaticZoneId() method to make a separate call to the database, get the Id and use it in the other queries. I gust modified the query to not generate exception when there is no data in the corresponding table:
protected long GetClimaticZoneId()
{
return dbContext.ClimaticZonesLogs.OrderByDescending(cz => cz.Timestamp).FirstOrDefault()?.ClimaticZoneId??0;
}

LINQ: How to Select specific columns using IQueryable()

I need to select only two columns from Hospital table, HospitalId and Name.
i tried the below code it selects all columns from Hospital table which lead to slow performance. Please help me to select only two columns from Hospital table
public HttpResponseMessage GetAvailableHospitalsByAjax(System.Guid? DirectorateOfHealthID = null, System.Guid? UnitTypeID = null, string DeviceTypeIDs = null)
{
Context db = new Context();
var query = db.Hospitals.AsQueryable();
if (UnitTypeID != null)
{
query = query.Where(j => j.HospitalDepartments.Any(www => www.Units.Any(u => u.UnitTypeID == UnitTypeID)));
}
if (DirectorateOfHealthID != null)
{
query = query.Where(h => h.DirectorateHealthID == DirectorateOfHealthID);
}
query = query.Where(j => j.HospitalDepartments.Any(u => u.Units.Any(d => d.Devices.Any(s => s.Status == Enums.DeviceStatus.Free)))
&& j.HospitalDepartments.Any(hd => hd.Units.Any(u => u.Beds.Any(b => b.Status == Enums.BedStatus.Free))));
var list = query.ToList().Select(w => new HospitalInfo()
{
Id = w.ID,
Name = w.Name
}).ToList();
return Request.CreateResponse(HttpStatusCode.OK, list);
}
IQueryable<T> executes select query on server side with all filters. Hence does less work and becomes fast.
IEnumerable<T> executes select query on server side, load data in-memory on client side and then filter data. Hence does more work and becomes slow.
List<T> is just an output format, and while it implements IEnumerable<T>, is not directly related to querying.
So,
var list = query.ToList().Select(w => new HospitalInfo()
{
Id = w.ID,
Name = w.Name
}).ToList();
In your code you use query.ToList(). This means at first it pull all data into memory then apply Select query.If you want to retrieve HospitalID and Name then remove ToList() then your code like
var list = query.Select(w => new HospitalInfo
{
Id = w.ID,
Name = w.Name
}).ToList();
Remove the ToList call before the projection:
var list = query.Select(w => new HospitalInfo()
{
Id = w.ID,
Name = w.Name
}).ToList();
With that ToList call you are materializing your query before do the projection
Because you do query.ToList() this materialises the entire query with all columns into memory. It's actually a bad habit to get into. Instead, remove that, you already have it at the end anyway. The Select projection you have will only retrieve the relevant columns though:
var list = query.Select(w => new HospitalInfo()
{
Id = w.ID,
Name = w.Name
}).ToList();

Linq to Object referencing a list

I have two disparate datasources, one has details about Clients the other the Site that only has a ClientID but due to partial migration of a system I can't join them at a database level (yet, that will eventually happen!):
var clients = _clientService.GetClientSummary(true);
var results = context.Sites.AsNoTracking().OrderByDescending(s => s.Id).
Skip((pageIndex - 1) * pageSize).Take(pageSize);
result.Content = pageResult.Select(a => new QuoteSearch
{
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
Running the code above returns an error
"LINQ to Entities does not recognize the method
'CertsAssured.Model.Client.ClientSummary
Find(System.Predicate`1[CertsAssured.Model.Client.ClientSummary])' "
I can write the code after this step to perform the task but would then have to save the ClientId in to my object to then iterate through. Is there a way of getting the info from the client list during the Select method?
Thanks
After the database filtering/paging is set up, you can use AsEnumerable to convert the IQueryable result to an in memory IEnumerable where you can do lookups against clients;
result.Content = pageResult
.AsEnumerable()
.Select(a => new QuoteSearch
{
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();
If there are many database fields and you don't want all of the fetched from the database, you can filter the fields on the IQueryable first, something like;
result.Content = pageResult
.Select(a => new // This filters in the database
{ // to an anonymous type
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
ClientId = a.ClientId
})
.AsEnumerable() // Convert to an IEnumerable
.Select(a => new QuoteSearch // This is done in-memory
{ // generating the real type
Accepted = a.Accepted,
Created = a.Created,
Id = a.Id,
Customer = clients.Find(b => b.Id == a.ClientId).Name
}).ToList();

Entity Navigation Property IQueryable cannot be translated into a store expression

im using Entity Framework designer first and I need to create custom Model Objects starting from the db objects.
I don't want to use IEnumerable cause it will query too many fields.
The goal is to remove the inner select within this function:
using (var db = new dbEntities())
{
var departments= db.departments
.Include(p => p.employee)
.Where(...)
.Select(p => new CustomDepartmentModel()
{
ID = p.ID,
Employees = p.employee
.Select(q => new CustomEmployeeModel()
{
ID = q.ID,
Name= q.Name
}).ToList()
});
return departments.ToList();
}
by using this function:
public static IQueryable<CustomEmployeeModel> ToModel(this IQueryable<employee> Employee)
{
return Employee.Select(u => new CustomEmployeeModel()
{
ID = u.ID,
Name = u.Name
});
}
But I always get the error: "LINQ to Entities does not recognize the method ToModel".
I did try to use it in these ways without luck:
Employees = p.employee.AsQueryable().ToModel().ToList() //1
Employees = db.Entry(p).Collection(f => f.employee).Query().ToModel().ToList() //2
I think that I need to use something like this:
public static System.Linq.Expressions.Expression<Func<IQueryable<employee>, IQueryable<CustomEmployeeModel>>> ToModel()
{
return p => p.Select(u => new CustomEmployeeModel()
{
ID = u.ID,
Name = u.Name
});
}
but I really can't figure out how to use it.
I think that I need to use something like this: [snip] but I really can't figure out how to use it.
That's exactly what you need, but you also need LINQKit to make the query work. With it, your code would look like this:
var toModel = ToModel();
var departments2 = db.departments
.AsExpandable()
.Include(p => p.employee)
.Where(p => true)
.Select(p => new CustomDepartmentModel()
{
ID = p.ID,
Employees = toModel.Invoke(p.employee).ToList()
});
The problem is that LINQ to Entities is trying to translate your ToModel() method into a SQL query (since that's what LINQ to Entities is supposed to do), and it can't find a way to do it, hence the error that you're seeing.
In order for you to call ToModel() you'll need to have the information already come in from the database, which will then make any LINQ query a LINQ to Objects query, which will be more than able to do what you are asking for. You can do this by calling ToList() before calling ToModel().

How to select a single column with Entity Framework?

Is there a way to get the entire contents of a single column using Entity Framework 4? The same like this SQL Query:
SELECT Name FROM MyTable WHERE UserId = 1;
You can use LINQ's .Select() to do that. In your case it would go something like:
string Name = yourDbContext
.MyTable
.Where(u => u.UserId == 1)
.Select(u => u.Name)
.SingleOrDefault(); // This is what actually executes the request and return a response
If you are expecting more than one entry in response, you can use .ToList() instead, to execute the request. Something like this, to get the Name of everyone with age 30:
string[] Names = yourDbContext
.MyTable
.Where(u => u.Age == 30)
.Select(u => u.Name)
.ToList();
I'm a complete noob on Entity but this is how I would do it in theory...
var name = yourDbContext.MyTable.Find(1).Name;
If It's A Primary Key.
-- OR --
var name = yourDbContext.MyTable.SingleOrDefault(mytable => mytable.UserId == 1).Name;
-- OR --
For whole Column:
var names = yourDbContext.MyTable
.Where(mytable => mytable.UserId == 1)
.Select(column => column.Name); //You can '.ToList();' this....
But "oh Geez Rick, What do I know..."
Using LINQ your query should look something like this:
public User GetUser(int userID){
return
(
from p in "MyTable" //(Your Entity Model)
where p.UserID == userID
select p.Name
).SingleOrDefault();
}
Of course to do this you need to have an ADO.Net Entity Model in your solution.
You could use the LINQ select clause and reference the property that relates to your Name column.
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.
var name = dbContext.MyTable.Select(x => new { x.UserId, x.Name }).FirstOrDefault(x => x.UserId == 1)?.Name;
Above query will be converted to this:
Select Top (1) UserId, Name from MyTable where UserId = 1;
For multiple items you can simply chain Select after Where:
var names = dbContext.MyTable.Where(x => x.UserId > 10).Select(x => x.Name);
Use anonymous object inside Select if you need more than one properties.

Categories

Resources