Last active
December 21, 2015 00:59
-
-
Save leblancmeneses/6224002 to your computer and use it in GitHub Desktop.
Automapper EF IQueryable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
working expression.ToString(); | |
value(System.Data.Objects.ObjectQuery`1[Host.MockDB.CaseInformation]).MergeAs(AppendOnly).Select(x => new CaseInformationViewModel() {Id = x.Id, CaseNumber = x.CaseNumber, Title = x.ShortTitle, CaseType = IIF((x.CaseTypeDropDown == null), String.Empty, x.CaseTypeDropDown.Name), CaseType_Id = x.CaseTypeDropDown_Id, Category = IIF((x.CategoryDropDown == null), String.Empty, x.CategoryDropDown.Name), Category_Id = x.CategoryDropDown_Id, FilingAttorney = IIF((x.FilingAttorneyDropDown == null), String.Empty, x.FilingAttorneyDropDown.Name), FilingAttorney_Id = x.FilingAttorneyDropDown_Id, PaymentAccount = IIF((x.PaymentAccountDropDown == null), String.Empty, x.PaymentAccountDropDown.Name), PaymentAccount_Id = x.PaymentAccountDropDown_Id, Location = IIF((x.LocationDropDown == null), String.Empty, x.LocationDropDown.Name), Location_Id = x.LocationDropDown_Id, LowerCourt = IIF((x.LowerCourtDropDown == null), String.Empty, x.LowerCourtDropDown.Name), LowerCourt_Id = x.LowerCourtDropDown_Id}) | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Data.Entity; | |
using System.Text.RegularExpressions; | |
using AutoMapper; | |
using AutoMapper.QueryableExtensions; | |
using Host.MockDB; | |
namespace Host.Mocks | |
{ | |
public partial class CaseInformation | |
{ | |
public int Id { get; set; } | |
public string CaseNumber { get; set; } | |
public string ShortTitle { get; set; } | |
public int? LocationDropDown_Id { get; set; } | |
public int? CategoryDropDown_Id { get; set; } | |
public int? CaseTypeDropDown_Id { get; set; } | |
public int? LowerCourtDropDown_Id { get; set; } | |
public int? FilingAttorneyDropDown_Id { get; set; } | |
public int? PaymentAccountDropDown_Id { get; set; } | |
[ForeignKey("CaseTypeDropDown_Id")] | |
public virtual CaseTypeDropDown CaseTypeDropDown { get; set; } | |
[ForeignKey("CategoryDropDown_Id")] | |
public virtual CategoryDropDown CategoryDropDown { get; set; } | |
[ForeignKey("FilingAttorneyDropDown_Id")] | |
public virtual FilingAttorneyDropDown FilingAttorneyDropDown { get; set; } | |
[ForeignKey("LocationDropDown_Id")] | |
public virtual LocationDropDown LocationDropDown { get; set; } | |
[ForeignKey("LowerCourtDropDown_Id")] | |
public virtual LowerCourtDropDown LowerCourtDropDown { get; set; } | |
[ForeignKey("PaymentAccountDropDown_Id")] | |
public virtual PaymentAccountDropDown PaymentAccountDropDown { get; set; } | |
} | |
[DisplayName("Enter the Details for the New Case")] | |
public class CaseInformationViewModel | |
{ | |
public int Id { get; set; } | |
[DisplayName("Location")] | |
public int? Location_Id { get; set; } | |
[DisplayName("Category")] | |
public int? Category_Id { get; set; } | |
[DisplayName("Case Type")] | |
public int? CaseType_Id { get; set; } | |
[DisplayName("Lower Court/Agency")] | |
public int? LowerCourt_Id { get; set; } | |
[DisplayName("Lower Court/Agency Case #")] | |
[Required] | |
[RegularExpression(@"^\d+$")] | |
public string CaseNumber { get; set; } | |
[DisplayName("Short Title")] | |
[Required] | |
[StringLength(10)] | |
public string Title { get; set; } | |
[DisplayName("Filing Attorney")] | |
public int? FilingAttorney_Id { get; set; } | |
[DisplayName("Payment Account")] | |
public int? PaymentAccount_Id { get; set; } | |
public string Location { get; set; } | |
public string Category { get; set; } | |
public string CaseType { get; set; } | |
public string LowerCourt { get; set; } | |
public string FilingAttorney { get; set; } | |
public string PaymentAccount { get; set; } | |
} | |
public static class AutoMapperMockConfig | |
{ | |
public static void Configure() | |
{ | |
Mapper.CreateMap<CaseInformation, CaseInformationViewModel>() | |
.ForMember(dest => dest.Id, op => op.MapFrom(src => src.Id)) | |
.ForMember(dest => dest.CaseNumber, op => op.MapFrom(src => src.CaseNumber)) | |
.ForMember(dest => dest.Title, op => op.MapFrom(src => src.ShortTitle)) | |
//.ForMember(dest => dest.CaseType, op => op.MapFrom(src => src.CaseTypeDropDown == null ? string.Empty : src.CaseTypeDropDown.Name)) | |
.ForMember(dest => dest.CaseType_Id, op => op.MapFrom(src => src.CaseTypeDropDown_Id)) | |
//.ForMember(dest => dest.Category, op => op.MapFrom(src => src.CategoryDropDown == null ? string.Empty : src.CategoryDropDown.Name)) | |
.ForMember(dest => dest.Category_Id, op => op.MapFrom(src => src.CategoryDropDown_Id)) | |
//.ForMember(dest => dest.FilingAttorney, op => op.MapFrom(src => src.FilingAttorneyDropDown == null ? string.Empty : src.FilingAttorneyDropDown.Name)) | |
.ForMember(dest => dest.FilingAttorney_Id, op => op.MapFrom(src => src.FilingAttorneyDropDown_Id)) | |
//.ForMember(dest => dest.PaymentAccount, op => op.MapFrom(src => src.PaymentAccountDropDown == null ? string.Empty : src.PaymentAccountDropDown.Name)) | |
.ForMember(dest => dest.PaymentAccount_Id, op => op.MapFrom(src => src.PaymentAccountDropDown_Id)) | |
//.ForMember(dest => dest.Location, op => op.MapFrom(src => src.LocationDropDown == null ? string.Empty : src.LocationDropDown.Name)) | |
.ForMember(dest => dest.Location_Id, op => op.MapFrom(src => src.LocationDropDown_Id)) | |
//.ForMember(dest => dest.LowerCourt, op => op.MapFrom(src => src.LowerCourtDropDown == null ? string.Empty : src.LowerCourtDropDown.Name)) | |
.ForMember(dest => dest.LowerCourt_Id, op => op.MapFrom(src => src.LowerCourtDropDown_Id)); | |
} | |
} | |
public class RandomCaseRepository : ICaseRepository | |
{ | |
private readonly MockDataContext _mockDataEntities; | |
public RandomCaseRepository(MockDataContext mockDataEntities) | |
{ | |
_mockDataEntities = mockDataEntities; | |
} | |
public IEnumerable<CaseInformationViewModel> GetAll() | |
{ | |
var cases = _mockDataEntities.CaseInformations | |
.Include(x => x.CaseTypeDropDown) | |
.Include(x => x.CategoryDropDown) | |
.Include(x => x.FilingAttorneyDropDown) | |
.Include(x => x.LocationDropDown) | |
.Include(x => x.LowerCourtDropDown) | |
.Include(x => x.PaymentAccountDropDown).Take(10).Project().To<CaseInformationViewModel>(); | |
return cases; | |
} | |
public bool TryGetBy(long? id, out CaseInformationViewModel caseInformationViewModel) | |
{ | |
caseInformationViewModel = null; | |
if (id.HasValue == false) | |
{ | |
return false; | |
} | |
caseInformationViewModel = GetAll().Single(x => x.Id == id.Value); | |
return true; | |
} | |
public void AddOrUpdate(CaseInformationViewModel viewModel) | |
{ | |
if (viewModel.Id == 0) | |
{ | |
var caseInfo = Mapper.Map<CaseInformationViewModel, CaseInformation>(viewModel); | |
_mockDataEntities.CaseInformations.Add(caseInfo); | |
} | |
else | |
{ | |
var caseInfo = _mockDataEntities.CaseInformations.Single(x => x.Id == viewModel.Id); | |
Mapper.Map(viewModel, caseInfo); | |
_mockDataEntities.SaveChanges(); | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
working expression.ToString(); | |
value(System.Data.Objects.ObjectQuery`1[Host.MockDB.CaseInformation]).MergeAs(AppendOnly).Select(x => new CaseInformationViewModel() {Id = x.Id, CaseNumber = x.CaseNumber, Title = x.ShortTitle, CaseType = IIF((x.CaseTypeDropDown == null), String.Empty, x.CaseTypeDropDown.Name), CaseType_Id = x.CaseTypeDropDown_Id, Category = IIF((x.CategoryDropDown == null), String.Empty, x.CategoryDropDown.Name), Category_Id = x.CategoryDropDown_Id, FilingAttorney = IIF((x.FilingAttorneyDropDown == null), String.Empty, x.FilingAttorneyDropDown.Name), FilingAttorney_Id = x.FilingAttorneyDropDown_Id, PaymentAccount = IIF((x.PaymentAccountDropDown == null), String.Empty, x.PaymentAccountDropDown.Name), PaymentAccount_Id = x.PaymentAccountDropDown_Id, Location = IIF((x.LocationDropDown == null), String.Empty, x.LocationDropDown.Name), Location_Id = x.LocationDropDown_Id, LowerCourt = IIF((x.LowerCourtDropDown == null), String.Empty, x.LowerCourtDropDown.Name), LowerCourt_Id = x.LowerCourtDropDown_Id}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment