I am trying to fill a list with my categories data.
My categories have cascade format. They are formatted recursively in my database.
Here is my model object.
public class CategoriesDTO
{
public int Id { get; set; }
public int Level { get; set; }
public string Name { get; set; }
public List<CategoriesDTO> Subs { get; set; }
}
So, in my business layer, i am trying to call this method like this:
DAO.Categories(0);
it will start with "Level==0" condition and then it will go on..
but i couldn't manage the Data Access Layer. I tried this:
public List<CategoriesDTO> Categories(int PrmLevel)
{
List<CategoriesDTO> DTO = new List<CategoriesDTO>();
//DB is my dbcontext.
DTO = DB.Categories.Select(x => new CategoriesDTO()
{
Id = x.Id,
Level = x.Level,
Name = x.Name
}).Where(y => y.Level == PrmLevel).ToList();
foreach (var item in DTO)
{
//im stucked
}
return DTO;
}
}
public List<CategoriesDTO> Categories(int PrmLevel)
{
List<CategoriesDTO> DTO = new List<CategoriesDTO>();
//DB is my dbcontext.
DTO = DB.Categories.Select(x => new CategoriesDTO()
{
Id = x.Id,
Level = x.Level,
Name = x.Name
}).Where(y => y.Level == PrmLevel).ToList();
foreach (var item in DTO)
{
item.Subs = ((PrmLevel + 1) <= MaxLevel) ? Categories(PrmLevel + 1) : null;
}
return DTO;
}
}
public List<CategoriesDTO> Categories(int PrmLevel)
{
List<CategoriesDTO> DTO = new List<CategoriesDTO>();
//DB is my dbcontext.
DTO = DB.Categories.Select(x => new CategoriesDTO()
{
Id = x.Id,
Level = x.Level,
Name = x.Name
}).Where(y => y.Level == PrmLevel).ToList();
foreach (var item in DTO)
{
int CountSub = 0;
CountSub = DB.Categories.Where(x => x.Level == item.Id).ToList().Count();
if (CountSub!=0)
{
item.Subs = Categories(item.Id).ToList();
}
}
return DTO;
}
}
Related
I have a batch of users defined with that model.
I need to iterate inside Manager nesting while manager is not null and create Departments entities with parents relation. I have an example of code what I already have, so I post it here.
What I already have:
public class AdUserModel
{
public string UserName { get; set; }
public AdUserModel Manager { get; set; }
}
...
List<UserDepartment> userDepartmentsToAdd = new List<UserDepartment>();
List<Department> newDeps = new List<Department>();
RecurseDepartments(adUser);
var userDepartment = new UserDepartment
{
User = user,
PositionName = adUser.PositionName,
Department = newDeps.FirstOrDefault(x => x.Name == adUser.DepartmentName),
IsHeadUser = user.SubordinateUsers?.Any() ?? false
};
userDepartmentsToAdd.Add(userDepartment);
void RecurseDepartments(AdUserModel model)
{
var department = new Department();
var existDep = newDeps.FirstOrDefault(x => x.Name ==
model.DepartmentName);
if (existDep == null)
{
department.Name = model.DepartmentName;
}
if (model.Manager is not null)
{
if (newDeps.FirstOrDefault(x => x.Name == model.Manager.DepartmentName) == null)
{
var parentDepartment = new Department
{
Name = model.Manager.DepartmentName
};
department.ParentDepartment = existDep ?? department;
if (existDep == null)
{
newDeps.Add(department);
}
newDeps.Add(parentDepartment);
}
if (model.Manager.DepartmentName != model.DepartmentName)
{
RecurseDepartments(model.Manager);
}
}
}
Thanks for any help in advance, being stuck here for some reason.
First of all look at my code: this is ConceptDto:
public Guid ConceptId { get; set; }
public string ConceptName { get; set; }
public string ConceptLatinName { get; set; }
public List<ConceptSubDto> ConceptSubSidiary { get; set;}
This is the ConceptSubDto:
public Guid ConceptSubId { get; set; }
public string ConceptSubName { get; set; }
and I have domains like that.
Now this is my application layer that have logic I want to get these by id and return just one ConceptDto but I don't have any idea have to map these dtos with domain models:
public async Task<ConceptManagementDto> GetConceptById(Guid id)
{
var concept = await _conceptManagementRepository.Query()
.Include(x => x.conceptSubSidiaries)
.GetOneAsync(x => x.Id == id);
return new ConceptManagementDto
{
ConceptManagementId = concept.Id,
ConceptName = concept.ConceptName,
ConceptLatinName = concept.ConceptLatinName,
ConceptSubSidiary = ??
};
}
This query should effective if your DTO has less fields:
public async Task<ConceptManagementDto> GetConceptById(Guid id)
{
return await _conceptManagementRepository.Query()
.Where(x => x.Id == id)
.Select(x = new ConceptManagementDto
{
ConceptManagementId = x.Id,
ConceptName = x.ConceptName,
ConceptLatinName = x.ConceptLatinName,
ConceptSubSidiary = x.ConceptSubSidiaries
.Select(sub => new ConceptSubDto
{
ConceptSubId = sub.ConceptSubId,
ConceptSubName = sub.ConceptSubName
})
.ToList()
}).First();
}
Initially I was using automapper for this but its seems way harder for me to implement it.
Basically, I just want to return an empty list instead of null values. I can do this on projects level but not on teammates level. The API must not return a null because the UI that consumes it will have an error.
Sample of my implementation below:
Projects = !Util.IsNullOrEmpty(x.Projects) ? x.Projects : new List<ProjectsDto>(),
Ill highly appreciate if someone can guide me on how to manually map this with null/empty checking.
If you can also provide and example using automapper that too will be very helpful.
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public List<ProjectsDto> Projects { get; set; }
}
public class ProjectsDto
{
public string Status { get; set; }
public List<TeammatesDto> Teammates { get; set; }
}
public class TeammatesDto
{
public string TeammateName { get; set; }
public string PreviousProject { get; set; }
}
//Get by Id
var employee = await _context.Employees
.Where(x => x.id.Equals(request.Id)
.FirstOrDefaultAsync(cancellationToken);
//Map employee
EmployeeDto ret = new EmployeeDto()
{
Id = employee.id,
Name = employee.Name,
Projects = null //TODO: map manually
}
//Get all employees
var employees = await _context.Employees.AsNoTracking()
.ToListAsync(cancellationToken);
//Map here
IList<EmployeeDto> list = new List<EmployeeDto>();
foreach (var x in employees)
{
EmployeeDto dto = new EmployeeDto()
{
Id = x.id,
Name = x.Name,
Projects = null //TODO: map manually
};
list.Add(dto);
}
return list;
Instead of materializing full entities, do the following:
var query = _context.Employees
.Select(e = new EmployeeDto
{
Id = e.id,
Name = e.Name,
Projects = e.Projects.Select(p => new ProjectDto
{
Status = p.Status,
Templates = p.Templates.Select(t => new TemplateDto
{
TeammateName = t.TeammateName,
PreviousProject = t.PreviousProject
}).ToList()
}).ToList()
}
);
var result = await query.ToListAsync();
I Have the Following Model
ProductDetails.cs
public class ProductDetails
{
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductCodeID { get; set; }
public string CategoryName { get; set; }
public List<ProductDetails> lstProductDetails { get; set; }
}
Need to fill this model class with the query results that I'm getting from the DB
This is what I tried ..
var results = from a in db.ProductInfoes where productCodeID.Equals(productCodeID)
select new ProductDetails
{
ProductName = a.Product_Name.ToString(),
ProductCategoryID= a.Category_ID.ToString(),
ProductDescription = a.Product_Description.ToString()
};
What's the best way to update model class with query results?
you can try extending ProductInfoes and using automapper.
public **partial class** ProductInfoes {
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<AuthorModel, AuthorDTO>();
});
IMapper iMapper = config.CreateMapper();
public ProductDetails Map()
{
return iMapper.Map<ProductInfoes , ProductDetails>(this);
}
}
Remove the lstProductDetails property from the ProductDetails class.
to get all data (results will be of type: List<ProductDetails>)
var results = db.ProductInfoes
.Select(x => new ProductDetails()
{
ProductName = x.Product_Name.ToString(),
ProductCategoryID= x.Category_ID.ToString(),
ProductDescription = x.Product_Description.ToString()
}).ToList();
to get one record (result will be of type: ProductDetails):
var result = db.ProductInfoes.FirstOrDefault(x => x.productCodeID.Equals(productCodeID))
.Select(x => new ProductDetails()
{
ProductName = x.Product_Name.ToString(),
ProductCategoryID= x.Category_ID.ToString(),
ProductDescription = x.Product_Description.ToString()
};
(Add namespace System.Linq)
public ProductDetails getSelectedProductDetails(string productCodeID)
{
try
{
NaturesKingdomUKEntities db = new NaturesKingdomUKEntities();
List<ProductInfo> lst_ProductInfo = db.ProductInfoes.Where(x => x.Product_Code_ID.Equals(productCodeID)).ToList();
ProductDetails prd = new ProductDetails();
prd = lst_ProductInfo
.Where(y => y.Product_Code_ID.Equals(productCodeID))
.Select(x => new ProductDetails { ProductName = x.Product_Name, ProductCodeID = x.Product_Code_ID, UnitPrice=Convert.ToDouble(x.Unit_Price), ProductDescription=x.Product_Description, ProductAdditionalDescription=x.Product_Additional_Description, ProductType=x.Product_Type })
.FirstOrDefault();
return prd;
}
catch (Exception ex)
{
throw;
}
}
Finally it works with this way. Thanks a lot guys #Matt.G
I have two DTOs:
public class MasterDTO
{
public int Id { get; set; }
public string Name { get; set; }
public List<DetailDTO> Details { get; set; }
}
public class DetailDTO
{
public int Id { get; set; }
public string DetailName { get; set; }
}
Also, I have a function:
using (var context = new Context())
{
var r = context.MasterData
.Select(d => new MasterDTO
{
Id = d.Id,
Name = d.Name,
}
}
I need to fill the list of DetailDTOs too and do it in a single request.
At this moment, I have to get list of DetailsData data and add it through foreach to the MasterDTO, which, of course causes a lot of requests to the database server.
Is there a better solution?
In your data call, do an eager load on your DetailData.
Example:
var r = context.MasterData.Include("DetailData")
DetailData should be the name of your navigation property attached to your MasterData entity.
This will cause detail data to be pulled along with your call for MasterData.
The full call may look something like this:
using (var context = new Context())
{
context.LazyLoadingEnabled = false;
var r = context.MasterData.Include("DetailData")
.Select(d => new MasterDTO()
{
Id = d.Id,
Name = d.Name,
Details = d.Details.Select(dt => new DetailDTO()
{
Id = dt.Id,
DetailName = dt.DetailName
})
});
}