我有以下設置LINQ to SQL和自動映射
- LINQ到SQL數據上下文
- 數據傳輸對象(DTO的),我用它來從我的業務層將數據傳遞到我的UI。
我的DTO的典型用法是將數據庫中的表連接起來,這樣我就可以向我的UI傳遞一個包含連接結果的數據對象。
我有類似下面的代碼(這是顯著從實際的代碼減少SO張貼在)...
IQueryable<CustomerEventDto> query = (
from ce in db.CustomerEvents
join cet in db.CustomerEventTypes on ce.CustomerEventTypeId equals cet.CustomerEventTypeId
join c in db.Customers on ce.PidCreating equals c.Pid
join c in db.Countries on ce.CountryId equals c.CountryId
join t in db.TimeZones on ce.TimeZoneId equals t.TimeZoneId
where ce.Pid == pid
select new CustomerEventDto()
{
ApprovedHR = ce.ApprovedHR,
City = ce.City,
Closed = ce.Closed,
CountryId = ce.CountryId,
CountryCode = c.CodeISO3166Alpha2,
CreatorForename = c.CustomersUnique.Forename,
CreatorSurname = c.CustomersUnique.Surname,
CreatorUsername = c.UserName,
Email = ce.Email,
EventDateTime = ce.DateTime,
EventType = ce.CustomerEventTypeId,
EventTypeDescription = cet.Detail,
LockedOutSecurity = ce.LockedOutSecurity,
LockedOutSuspension = ce.LockedOutSuspension,
TimeZoneDifference = t.Difference,
TimeZoneId = ce.TimeZoneId,
TimeZoneName = t.ZoneName
});
query = query
.OptionalWhere(from, ce => (ce.EventDateTime >= from.StartOfDayNullable()))
.OptionalWhere(to, ce => (ce.EventDateTime <= to.StartOfDayNullable()))
.OptionalWhere(eventType, ce => (ce.EventType == eventType));
return query.ToList();
有沒有我可以使用自動映射器來做到這一點映射的方法?
你可能想整理你的代碼,你的縮進太大了。它現在的樣子真是難以理解。 –