Reputation: 1918
i'm starter in Repository Pattern in EntityFreamework in Bussiness Layer
i Have Get Methods
public IQueryable<Contract> GetAll()
{
var repository = new ContractsRepository(this.Context);
return repository.GetAll();
}
public IQueryable<Contract> GetAll(Func<Contract, bool> predicate)
{
var repository = new ContractsRepository(this.Context);
return repository.GetAll(predicate);
}
These are good,But I want to do computing on the field when a function return value to do what?
For example, I have 2 tables Table 1 and Table 2
I want the Bussiness layer Table1 Get method to write the following
Field1 ,Field2,Field1+Field3,Field2+Field4
thanks
Upvotes: 0
Views: 2033
Reputation: 126587
I disagree with Tony Hopkinson's comment. You should not add a read-only property to your Contact
class, because such a property cannot be used in LINQ to Entities, and hence aren't suitable for methods which return IQueryable
.
Instead, do something like:
public IQueryable<ContractSumsModel> GetSums()
{
var repository = new ContractsRepository(this.Context);
return from c in repository.GetAll()
select new ContractSumsModel
{
Field1 = c.Field1,
Field2 = c.Field2,
Fields1and3 = Field1 + Field3,
Fields2and4 = Field2 + Field4
};
}
The nice thing about this is that you can further compose the query:
var q = MyBusinessLayer.GetSums().OrderBy(c => c.Fields1and3).Take(5);
...and the whole thing is done in SQL.
Upvotes: 4