How to use Projection in LINQ to convert entity to DTO class - c#

I have Linq script and I want to use projection class to get data to DTO type. I got the example for lambda expersion but getting error on the LINQ script.
Linq Script:
public class EziTransactionDto
{
... other properties
public static Expression<Func<EziTransactionEntity, EziTransactionDto>> Projection()
{
return eziTransactionDto => new EziTransactionDto
{
EziTransactionId = eziTransactionDto.Id,
LoginSiteID = eziTransactionDto.LoginSiteID,
WorkCodes = eziTransactionDto.WorkCodes
};
}
Linq query:
var ts = (from transaction in _eziTransactionRepository.GetAll<EziTransactionEntity>()
where transaction.LoginErrorCode != 0
select transaction
).Select(EziTransactionDto.Projection);
Error:

I guess the keyword Expression is odd there.
Try this:
// public static Expression<Func<EziTransactionEntity, EziTransactionDto>> Projection()
public static Func<EziTransactionEntity, EziTransactionDto> Projection()
{
return eziTransactionDto => new EziTransactionDto
{
EziTransactionId = eziTransactionDto.Id,
LoginSiteID = eziTransactionDto.LoginSiteID,
WorkCodes = eziTransactionDto.WorkCodes
};
}

After the first Select your IQueryable has already fetched the data to the local process, and made it an IEnumerable.
You could do this conversion in your Select statement:
var eziTransactionDtos = _eziTransactionRepository.EziTransactionEntities
.Where(eziTransactionEntity => eziTransationEntity.LoginErrorCode != 0)
.Select(eziTransactionEntity => new EziTransactionDto
{
EziTransactionId = eziTransactionDto.Id,
LoginSiteID = eziTransactionDto.LoginSiteID,
WorkCodes = eziTransactionDto.WorkCodes,
});
However, if you need to convert EziTransactionEntities to EziTransactionDtos on several places, it is a good idea to create a reusable extension method for IQueryable<EziTransactionEntities>.
If you are not familiar with extension methods, see extension methods demystified
public static IQueryable<EziTransactionDto> ToEziTransactionDto(
this IQueryable<EziTransactionEntity> eziTransactionEntities)
{
return eziTransactionEntities.Select(eziTransactionEntity => new EziTransactionDto
{
EziTransactionId = eziTransactionDto.Id,
LoginSiteID = eziTransactionDto.LoginSiteID,
WorkCodes = eziTransactionDto.WorkCodes,
});
Usage:
var eziTransactionDtos = eziTransactionRepository.EziTransactionEntities
.Where(eziTransactionEntity => eziTransationEntity.LoginErrorCode != 0)
.ToEziTransactionDtos();
Reusable:
var transactionWithoutWorkCodes = eziTransactionRepository.EziTransactionEntities
.Where(eziTransactionEntity => eziTransationEntity.WorkCode == null)
.ToEziTransactionDtos();
Easy to unit test:
List<EziTransactionEntity> testItems = ...
List<EziTransactionDto> expectedResults = ...
var testResults = testItems.AsQueryable().ToEziTransactionDtos();
Assert.AreQual(expectedResults, testResults, unorderedTransactionsComparer);
Easy to maintain: if you add / remove / change one property of this conversion, you'll only have to do this on one location

fond the answer
public static Expression<Func<EziTransactionEntity, EziTransactionDto>> Projection
{
get
{
return eziTransactionDto => new EziTransactionDto
{
EziTransactionId = eziTransactionDto.Id,
LoginSiteID = eziTransactionDto.LoginSiteID,
WorkCodes = eziTransactionDto.WorkCodes
};
}
}
..
var ts = (from transaction in _eziTransactionRepository.GetAll<EziTransactionEntity>()
select transaction
).Select(EziTransactionDto.Projection).ToList();

Related

how can I reduce 2 select to 1 select linq to gain performance?

my question is simple but I got stuck with something. Can you tell me how can I reduce 2 select into 1 select LINQ in c#? I am using CloudNative.CloudEvents NuGet package for cloud-native events.
var orderEvents = input
.Select(_ => new OrderDocument(_.Id, _.ToString()).ToOrderEvent())
.Select(_ =>
new CloudEvent()
{
Type = _.EventType,
Subject = _.Subject,
Source = _.Source,
Data = _
});
input is a parameter from cosmosDbTrigger it`s type : IReadOnlyList
OrderDocument.cs
public class OrderDocument
{
public string Id { get; private set; }
public string Json { get; private set; }
public OrderDocument(string id, string json)
{
Id = id;
Json = json;
}
public OrderEvent ToOrderEvent() => OrderEventHelper.ToOrderEvent(Json);
}
OrderEventHelper.cs
public static OrderEvent ToOrderEvent(string json)
{
ArgumentHelper.ThrowIfNullOrEmpty(json);
var orderEvent = JsonConvert.DeserializeObject<OrderEvent>(json);
var eventDefinition = OrderEvents.EventDefinitions.SingleOrDefault(_ => _.EventType == orderEvent.EventType);
return eventDefinition == null
? orderEvent
: new OrderEvent(
orderEvent.Id,
orderEvent.Source,
orderEvent.EventType,
orderEvent.Subject,
orderEvent.DataContentType,
orderEvent.DataSchema,
orderEvent.Timestamp,
JsonConvert.DeserializeObject(orderEvent.Payload.ToString(), eventDefinition.PayloadType),
orderEvent.TraceId);
}
linq extensions are basically for loops in the background. If you want to perform multiple actions against a list, perhaps making your own simple for loop where you can manage that yourself would work.
Your code:
var orderEvents = input
.Select(_ => new OrderDocument(_.Id, _.ToString()).ToOrderEvent())
.Select(_ =>
new CloudEvent()
{
Type = _.EventType,
Subject = _.Subject,
Source = _.Source,
Data = _
});
could be changed to:
// our result set, rather than the one returned from linq Select
var results = new List<CloudEvent>();
foreach(var x in input){
// create the order event here
var temporaryOrderEvent = new OrderDocument(x.Id, x.ToString()).ToOrderEvent();
// add the Cloud event to our result set
results.Add(new CloudEvent()
{
Type = temporaryOrderEvent .EventType,
Subject = temporaryOrderEvent .Subject,
Source = temporaryOrderEvent .Source,
Data = temporaryOrderEvent
});
}
where you then have a result list to work with.
If you wanted to keep it all in linq, you could instead perform all of your logic in the first Select, and ensure that it returns a CloudEvent. Notice here that you can employ the use of curly brackets in the linq statement to evaluate a function rather than a single variable value:
var orderEvents = input
.Select(x =>
{
// create the order event here
var temporaryOrderEvent = new OrderDocument(x.Id, x.ToString()).ToOrderEvent();
// return the Cloud event here
return new CloudEvent()
{
Type = temporaryOrderEvent .EventType,
Subject = temporaryOrderEvent .Subject,
Source = temporaryOrderEvent .Source,
Data = temporaryOrderEvent
};
});
How about putting conversion to OrderEvent and using ToCloudEvent in the same Select?
var orderEvents = input
.Select(_ => new OrderDocument(_.Id, _.ToString()).ToOrderEvent().ToCloudEvent())
public class OrderEvent
{
public CloudEvent ToCloudEvent()
{
new CloudEvent()
{
Type = this.EventType,
Subject = this.Subject,
Source = this.Source,
Data = this
};
}
}

LINQ to Objects - Reduce repetition - Map inside select statement

I am using LINQ to objects to run the multiple queries like below, I want to retain a separate method for each of the queries below but I want to have a function to map inside the select statement.
GetDTOCourseListActiveCourses
GetDTOCourseListWithValidDates
GetDTOCourseListRequirePayments
With each I am currently doing a manual mapping exercise inside the select for each query like below:
public IList<DTOCourse> GetDTOCourseListActiveCourses()
{
var query = _UoW.tblcoursRepo.All.Where(c => c.IsActive == true);
IList<DTOCourse> courselist = new List<DTOCourse>();
courselist = query.Select(x => new DTOCourse
{
////// BUT, I want to create a function here to do the mapping //
courseId = x.CourseID,
courseTitle = x.CourseTitle,
mainHeading = x.MainHeading.description,
courseType = x.ListType.description,
courseStatus = x.ListStatus.description,
orgId = x.OrgID.Value
}).ToList();
return courselist;
}
I have created the following method to map to the DTO class, this works fine when converting a single instance:
public DTOCourse MaptblcourseToDTOCourse(tblcours course)
{
DTOCourse dto = new DTOCourse
{
courseId = course.CourseID,
courseTitle = course.CourseTitle,
mainHeading = course.MainHeading.description,
courseType = course.ListType.description,
courseStatus = course.ListStatus.description,
orgId = course.OrgID.Value
};
return dto;
}
How can I combine this method to map within a select? I'm looking for something like below:
public IList<DTOCourse> GetDTOCourseListActiveCourses()
{
var query = _UoW.tblcoursRepo.All.Where(c => c.IsActive == true);
IList<DTOCourse> courselist = new List<DTOCourse>();
courselist = query.Select(x => new DTOCourse
{
MaptblcoursToDTOCourse(x)
}).ToList();
return courselist;
}
You are almost there. You can call your mapping method in your Select call like this:
courselist = query.Select(x => MaptblcoursToDTOCourse(x)).ToList();
Or even shorter:
courselist = query.Select(MaptblcoursToDTOCourse).ToList();

How do you reuse mapping functions on Nested entities in Entity Framework?

I have seen multiple questions that are similar to this one but I think my case is slightly different. I'm using EF6 to query the database and I'm using data projection for better queries.
Given that performance is very important on this project I have to make sure to just read the actual fields that I will use so I have very similar queries that are different for just a few fields as I have done this I have noticed repetition of the code so I'm been thinking on how to reuse code this is currently what I Have:
public static IEnumerable<FundWithReturns> GetSimpleFunds(this DbSet<Fund> funds, IEnumerable<int> fundsId)
{
IQueryable<Fund> query = GetFundsQuery(funds, fundsId);
var results = query
.Select(f => new FundWithReturns
{
Category = f.Category,
ExpenseRatio = f.ExpenseRatio,
FundId = f.FundId,
Name = f.Name,
LatestPrice = f.LatestPrice,
DailyReturns = f.FundDailyReturns
.Where(dr => dr.AdjustedValue != null)
.OrderByDescending(dr => dr.CloseDate)
.Select(dr => new DailyReturnPrice
{
CloseDate = dr.CloseDate,
Value = dr.AdjustedValue.Value,
}),
Returns = f.Returns.Select(r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
}).FirstOrDefault()
})
.ToList();
foreach (var result in results)
{
result.DailyReturns = result.DailyReturns.ConvertClosingPricesToDailyReturns();
}
return results;
}
public static IEnumerable<FundListVm> GetFundListVm(this DbSet<Fund> funds, string type)
{
return funds
.Where(f => f.StatusCode == MetisDataObjectStatusCodes.ACTIVE
&& f.Type == type)
.Select(f => new FundListVm
{
Category = f.Category,
Name = f.Name,
Symbol = f.Symbol,
Yield = f.Yield,
ExpenseRatio = f.ExpenseRatio,
LatestDate = f.LatestDate,
Returns = f.Returns.Select(r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
}).FirstOrDefault()
}).OrderBy(f=>f.Symbol).Take(30).ToList();
}
I'm trying to reuse the part where I map the f.Returns so I tried created a Func<> like the following:
private static Func<Return, ReturnValues> MapToReturnValues = r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
};
and then use like this:
public static IEnumerable<FundListVm> GetFundListVm(this DbSet<Fund> funds, string type)
{
return funds
.Where(f => f.StatusCode == MetisDataObjectStatusCodes.ACTIVE
&& f.Type == type)
.Select(f => new FundListVm
{
Category = f.Category,
Name = f.Name,
Symbol = f.Symbol,
Yield = f.Yield,
ExpenseRatio = f.ExpenseRatio,
LatestDate = f.LatestDate,
Returns = f.Returns.Select(MapToReturnValues).FirstOrDefault()
}).OrderBy(f=>f.Symbol).Take(30).ToList();
}
The compiler is ok with it but at runtime, it crashes and says: Internal .NET Framework Data Provider error 1025
I tried to convert the Func into Expression like I read on some questions and then using compile() but It didn't work using AsEnumerable is also not an option because It will query all the fields first which is what I want to avoid.
Am I trying something not possible?
Thank you for your time.
It definitely needs to be Expression<Func<...>>. But instead of using Compile() method (not supported), you can resolve the compile time error using the AsQueryable() method which is perfectly supported (in EF6, the trick doesn't work in current EF Core).
Given the modified definition
private static Expression<Func<Return, ReturnValues>> MapToReturnValues =
r => new ReturnValues { ... };
the sample usage would be
Returns = f.Returns.AsQueryable().Select(MapToReturnValues).FirstOrDefault()

Not expected result when use Linq Select

I implement unit tests for the engine and setup 2 different methods in the repository mock. So the first one works well, but when I do linq Select for the second one it returns 0, whereas I did setup to return specific object.
My code in Engine:
private readonly IEmployerWorkersClient _employerWorkersClient;
private readonly IJobsClient _jobsClient;
public EmployerWorkerEngine(IEmployerWorkersClient employerWorkersClient, IJobsClient jobsClient,)
{
_employerWorkersClient = employerWorkersClient;
_jobsClient = jobsClient;
}
public async Task<Grid<WorkerFiltered>> GetWorkersAsync(int employerId, GridState gridState)
{
var employerWorkers = await _employerWorkersClient.GetEmployerWorkersByEmployerIdAsync(employerId);
int? payrollId = null;
int? jobRoleId = null;
DateTime? bookedStart = null;
// !!!the following result is Empty collection!!!
List<JobRoleExtended> jobRoles = (await _jobsClient.GetJobRoleExtendedByEmployerWorkerIdsAsync(employerWorkers.Select(ew => ew.Id), payrollId, jobRoleId, bookedStart)).ToList();
// Other things
}
And hereafter my unit test class
private readonly EmployerWorkerEngine _employerWorkerEngine;
private readonly Mock<IEmployerWorkersClient> _employerWorkersClientMock;
private readonly Mock<IJobsClient> _jobClientMock;
public WorkersFilterTests()
{
_employerWorkersClientMock = new Mock<IEmployerWorkersClient>();
_jobClientMock = new Mock<IJobsClient>();
_employerWorkerEngine = new EmployerWorkerEngine(_employerWorkersClientMock.Object, _jobClientMock.Object,);
}
[Theory]
[InlineData(1)]
public async Task GetFilteredWorkersByEmployerIdSuccessSimple(int employerId)
{
// Arrange
const int employerWorkerId = 3;
var gridState = new GridState { Skip = 0, Take = 1 };
var employerWorkers = new List<EmployerWorker> { new EmployerWorker {EmployerId = 1, WorkerId = 2, Id = employerWorkerId} };
_employerWorkersClientMock.Setup(client => client.GetEmployerWorkersByEmployerIdAsync(employerId))
.ReturnsAsync(() => employerWorkers);
var jobRolesExtended = new List<JobRoleExtended>
{
new JobRoleExtended
{
EmployerWorkerId = employerWorkerId,
BookedStartDate = DateTime.UtcNow,
BookedEndDate = DateTime.UtcNow.AddDays(1),
Id = 5,
JobId = 8,
Name = "Job 5",
PayrollId = 10,
PayrollName = "Conduct"
}
};
_jobClientMock
.Setup(client => client.GetJobRoleExtendedByEmployerWorkerIdsAsync(employerWorkers.Select(ew => ew.Id), null, null, null))
.ReturnsAsync(() => jobRolesExtended);
}
So, during testing the following method returns Empty collection:
_jobsClient.GetJobRoleExtendedByEmployerWorkerIdsAsync() and I don't get why.
I assume that it related with the reference of the collection that generates linq Select, but even in this case, I don't know how to handle it for testing.
Can somebody help me with that?
does this help:
_jobClientMock.Setup(client => client.GetJobRoleExtendedByEmployerWorkerIdsAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>())
.ReturnsAsync(() => jobRolesExtended);
(do check if I got the types right)
if this is the case, then your missing something with your parameters.
With Moq you have to setup the expectations correctly or else when called the mock wont match and thus not perform as expected.
In the method under test you have
int? payrollId = null;
int? jobRoleId = null;
DateTime? bookedStart = null;
List<JobRoleExtended> jobRoles =
(await _jobsClient.GetJobRoleExtendedByEmployerWorkerIdsAsync(
employerWorkers.Select(ew => ew.Id), payrollId, jobRoleId, bookedStart)
)
.ToList();
The Select statement appears to be returning a collection of Ids (IEnumerable<int>) and you have already configured the first call correctly.
You now need to correctly configure the second call to expect that collection of int Ids
_jobClientMock
.Setup(_ => _.GetJobRoleExtendedByEmployerWorkerIdsAsync(
It.IsAny<IEnumerable<int>>(),
null,
null,
null)
)
.ReturnsAsync(() => jobRolesExtended);

Refactoring C# code - doing more within Linq

The code below is what I currently have and works fine. I feel that I could do more of the work I am doing in Linq instead of C# code.
Is there is anyone out there who can accomplish the same result with more Linq code and less C# code.
public List<Model.Question> GetSurveyQuestions(string type, int typeID)
{
using (eMTADataContext db = DataContextFactory.CreateContext())
{
List<Model.Question> questions = new List<Model.Question>();
List<Linq.Survey_Question> survey_questions;
List<Linq.Survey> surveys = db.Surveys
.Where(s => s.Type.Equals(type) && s.Type_ID.Equals(typeID))
.ToList();
if (surveys.Count > 0)
{
survey_questions = db.Survey_Questions
.Where(sq => sq.Survey_ID == surveys[0].ID).ToList();
foreach (Linq.Survey_Question sq in survey_questions)
{
Model.Question q = Mapper.ToBusinessObject(sq.Question);
q.Status = sq.Status;
questions.Add(q);
}
}
else
{
questions = null;
}
return questions;
}
}
Here is my Mapper function from my Entity to Biz Object
internal static Model.Question ToBusinessObject(Linq.Question q)
{
return new Model.Question
{
ID = q.ID,
Name = q.Name,
Text = q.Text,
Choices = ToBusinessObject(q.Question_Choices.ToList())
};
}
I want my mapper funciton to map the Question Status like so.
internal static Model.Question ToBusinessObject(Linq.Question q)
{
return new Model.Question
{
ID = q.ID,
Name = q.Name,
Text = q.Text,
Choices = ToBusinessObject(q.Question_Choices.ToList()),
Status = q.Survey_Questions[?].Status
};
}
? the issue is this function does not know which survey to pull the status from.
Instead of creating the biz object then setting the Status property in a foreach loop like so
foreach (Linq.Survey_Question sq in survey_questions)
{
Model.Question q = Mapper.ToBusinessObject(sq.Question);
q.Status = sq.Status;
questions.Add(q);
}
I would like to somehow filter the EntitySet<Survey_Question> in the q object above in the calling method, such that there would only be one item in the q.Survey_Questions[?] collection.
below is my database schema and business object schema
What I needed to do was setup a join.
public List<Model.Question> GetSurveyQuestions(string type, int typeID)
{
using (eMTADataContext db = DataContextFactory.CreateContext())
{
return db.Survey_Questions
.Where(s => s.Survey.Type.Equals(type) && s.Survey.Type_ID.Equals(typeID))
.Join(db.Questions,
sq => sq.Question_ID,
q => q.ID,
(sq, q) => Mapper.ToBusinessObject(q, sq.Status)).ToList();
}
}
And then overload my Mapper Function
internal static Model.Question ToBusinessObject(Linq.Question q, string status)
{
return new Model.Question
{
ID = q.ID,
Name = q.Name,
Text = q.Text,
Status = status,
Choices = ToBusinessObject(q.Question_Choices.ToList()),
};
}
from question in db.Survey_Questions
let surveys = (from s in db.Surveys
where string.Equals(s.Type, type, StringComparison.InvariantCultureIgnoreCase) &&
s.Type_ID == typeID)
where surveys.Any() &&
surveys.Contains(s => s.ID == question.ID)
select new Mapper.Question
{
ID = question.Id,
Name = question.Name,
Text = question.Text,
Choices = ToBusinessObject(question.Question_Choices.ToList()),
Status = question.Status
}
Does that get you on the right track?
Why are you duplicating all your classes? You could just extend the LINQ to SQL classes with your business logic - they are partial classes. This is somewhat against the purpose of an OR mapper - persisting business entities.

Categories

Resources