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

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()

Related

How to use Projection in LINQ to convert entity to DTO class

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();

Cannot convert Linq to SQL

So for a assignement I got I have to convert the data I get through an API into a object and save that object in the database. Since the object doesn't have a any property that is unique there are multiple primary key columns in my database.
So what I basically have to do is a 'merge' from to my database so if data has been changed to a update if data is new do an insert. For some reason one my linq queries for this is giving an error.
My database:
My Code:
public void Insert(List<MEDSU1> medsu1s)
{
var keys = medsu1s.Select(y => new { SUPP = y.S1SUPP?.Trim() ?? null, COMP = y.S1COMP?.Trim() ??null }).ToList();
List<MEDSU1> medusFromSql = MediusDataContext.MEDSU1s.Where(y =>
keys.Any(z => z.SUPP == y.S1SUPP && z.COMP == y.S1COMP)).ToList();
var toUpdate = from medsu1org in MediusDataContext.MEDSU1s
join sqlkeys in medusFromSql
on new
{
aa = medsu1org.S1COMP,
bb = medsu1org.S1SUPP
}
equals
new
{
aa = sqlkeys.S1COMP,
bb = sqlkeys.S1SUPP
}
select new
{
sql = medsu1org,
obj = sqlkeys,
};
toUpdate.ToList().ForEach(y =>
{
y.obj.S1COMP= y.sql.S1COMP;
y.obj.S1SUPP = y.sql.S1SUPP;
y.obj.S1SUNA = y.sql.S1SUNA;
y.obj.S1BAAC = y.sql.S1BAAC;
y.obj.S1VATN = y.sql.S1VATN;
y.obj.S1COUN = y.sql.S1COUN;
y.obj.S1PREF = y.sql.S1PREF;
y.obj.S1TAXD = y.sql.S1TAXD;
y.obj.S1CURR = y.sql.S1CURR;
y.obj.S1TYPE = y.sql.S1TYPE;
y.obj.S1ACNR = y.sql.S1ACNR;
y.obj.S1ACNM = y.sql.S1ACNM;
y.obj.S1EINV = y.sql.S1EINV;
y.obj.S1DLAY = y.sql.S1DLAY;
y.obj.S1TERM = y.sql.S1TERM;
y.obj.S1PYEE = y.sql.S1PYEE;
});
var toInsert = medsu1s.Except(toUpdate.Select(y => y.obj)).ToList();
MediusDataContext.MEDSU1s.InsertAllOnSubmit(toInsert);
MediusDataContext.SubmitChanges();
}
The part of the code that is giving me the error is the:
var keys = medsu1s.Select(y => new { SUPP = y.S1SUPP?.Trim() ?? null, COMP = y.S1COMP?.Trim() ??null }).ToList();
List<MEDSU1> medusFromSql = MediusDataContext.MEDSU1s.Where(y =>
keys.Any(z => z.SUPP == y.S1SUPP && z.COMP == y.S1COMP)).ToList();
I think it has to do with me using .Any incorrectly and converting it to a List but i dont know to fix it. Can someone explain me what I'm doing wrong?
ERROR im getting : "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator."
Since you're dealing with a compound key you'll have to create a query that basically looks like
Select *
From MEDSU1s
Where (S1SUPP == #S1SUPP1 && S1COMP == #S1COMP1)
OR (S1SUPP == #S1SUPP2 && S1COMP == #S1COMP2)
OR ....
To do that with Linq you'd have to build the expression procedurally. Note that I'm assuming the columns are both strings, so change the type of them as needed. Also I have no good way of testing this out but hopefully this can get you started towards a solution.
var queryableData = MediusDataContext.MEDSU1s;
var table = Expression.Parameter(typeof(MEDSU1s), "x");
var suppCol = Expression.Property(table, typeof(string), "S1SUPP");
var compCol = Expression.Property(table, typeof(string), "S1COMP");
Expression condition = Expression.Equal(Expression.Constant(1), Expression.Constant(0));
foreach (var x in keys)
{
var key1 = Expression.Equal(suppCol, Expression.Constant(x.SUPP));
var key2 = Expression.Equal(compCol, Expression.Constant(x.COMP));
var both = Expression.AndAlso(key1, key2);
condition = Expression.OrElse(condition, both);
}
var whereExpression = Expression.Call(
typeof(Queryable),
"Where",
new Type[] { queryableData.ElementType },
queryableData.Expression,
Expression.Lambda<Func<MEDSU1s, bool>>(
condition,
new ParameterExpression[] { table }));
var medusFromSql = queryableData.Provider.CreateQuery<MEDSU1s>(whereExpression).ToList();

Retrieve single record to model with EF Linq, does it need to loop to populate?

Most of the time I retrieve multiple records so I would end up doing this
var rpmuser = new List<rpm_scrty_rpm_usr>();
I have my List collection of properties from poco
So I typically use select new in my Linq statement
Then I use a foreach and loop over the records in which the List would get model.Add(new instance in each loop)
However , do I really need to be doing all this looping to populate?
Bigger question when i have a single record should I be needing to even do a loop at all?
public bool UpdateAllUsers(string user, string hash, string salt)
{
bool status = false;
var rpmuser = new rpm_scrty_rpm_usr();
var query = (from t in db.rpm_usr
.Where(z => z.usr_id == "MillXZ")
select new
{
t.usr_id,
t.usr_lnm,
t.usr_pwd,
t.usr_fnm,
t.salt,
t.inact_ind,
t.lst_accs_dtm,
t.lst_pwd_chg_dtm,
t.tel,
t.wwid,
t.email_id,
t.dflt_ste_id,
t.apprvr_wwid,
t.chg_dtm,
t.chg_usr_id,
t.cre_dtm,
t.cre_usr_id,
});
foreach(var s in query)
{
rpmuser.wwid = s.wwid;
rpmuser.usr_pwd = s.usr_pwd;
rpmuser.usr_lnm = s.usr_lnm;
rpmuser.usr_id = s.usr_id;
rpmuser.usr_fnm = s.usr_fnm;
rpmuser.tel = s.tel;
rpmuser.salt = s.salt;
rpmuser.lst_pwd_chg_dtm = rpmuser.lst_pwd_chg_dtm;
rpmuser.lst_accs_dtm = s.lst_accs_dtm;
rpmuser.inact_ind = s.inact_ind;
rpmuser.email_id = s.email_id;
rpmuser.apprvr_wwid = s.apprvr_wwid;
rpmuser.chg_dtm = s.chg_dtm;
rpmuser.chg_usr_id = s.chg_usr_id;
rpmuser.cre_usr_id = s.cre_usr_id;
rpmuser.dflt_ste_id = s.dflt_ste_id;
rpmuser.cre_dtm = s.cre_dtm;
}
DateTime dateTime = DateTime.Now;
try
{
rpmuser = db.rpm_usr.Find(rpmuser.usr_id);
rpmuser.usr_pwd = hash;
rpmuser.salt = salt;
db.SaveChanges();
status = true;
}
catch (Exception ex)
{
status = false;
}
return status;
}
I am not exactly sure what you want. Your method says Update All, but only seems to be attempting to update one record. So why don't you just do this?
try
{
var rpmuser = db.rpm_usr.Single(z => z.usr_id == "MillXZ");
rpmuser.usr_pwd = hash;
rpmuser.salt = salt;
db.SaveChanges();
status = true;
}
catch (Exception ex)
{
status = false;
}
You have a lot of redundant declarations unless I am missing something. In the case of the list you will do something like this:
var query = db.rpm_usr.Where(z => z.usr_id == "...some string...");
foreach(var item in query)
{
rpmuser.usr_pwd = ...some value...;
rpmuser.salt = ...some value...;
}
db.SaveChanges();
I can't stress this enough, Murdock's answer is absolutely the right way to fix the code you've shown. You are writing way too much code for what you're trying to accomplish.
However, to answer your question about whether you need to loop in other situations, you can get away from having to loop by doing the projection into a new type as part of your LINQ-to-Entities query. The looping still happens, you just don't see it.
var query = db.rpm_usr
.Where(z => z.usr_id == "MillXZ")
.AsEnumerable()
.Select(z => new rpm_scrty_rpm_usr()
{
usr_id = z.usr_id,
usr_lnm = z.usr_lnm,
// etc...
});
You would then finish the query off with a .Single(), .SingleOrDefault(), or .ToList() depending on whether you expected exactly one, one or zero, or a list. For example, in this case if you might find one or zero users with the name "MillXZ" you would write the following.
var query = db.rpm_usr
.Where(z => z.usr_id == "MillXZ")
.AsEnumerable()
.Select(z => new rpm_scrty_rpm_usr()
{
usr_id = z.usr_id,
usr_lnm = z.usr_lnm,
// etc...
})
.SingleOrDefault();

C# JOIN vs. Stored Procedure in Sql Server, Which one brings a better performance?

Let me change the question like this:
Which one of the below modes would increase the performance of an application?
A method in C# which performs about 6 JOIN statements and returns a value.
An stored procedure which performs 6 JOIN statements
Which of the above would be more efficient in terms of application performance?
Here is the Code:
public bool UserHasAccess(string userName, string actionName)
{
var decodedUserName = UtilityHands.GeneralTools.DecodeString(userName);
using (MAHAL_E_MA_Repository.UnitOfWork unit = new UnitOfWork())
{
var theUserID = unit.UserRepository.Get(filter: s => s.UserName.Equals(decodedUserName)).First().ID;
var userInRoleGroup = unit.RoleGroupUserRepository.Get(filter: r => r.UserInRole == theUserID).ToList();
var roleInRoleGroups = unit.RoleOfRoleGroupRepository.Get().ToList();
var serviceOfRoles = unit.ServiceOfRoleRepository.Get().ToList();
var serviceOfAction = unit.ServiceOfActionRepository.Get().ToList();
var roles = unit.RoleRepository.Get().ToList();
var service = unit.ServiceRepository.Get().ToList();
var actions = unit.ActionProviderRepository.Get().ToList();
var rolesOfRoleGroupJoin = userInRoleGroup.Join(roleInRoleGroups,
(u => u.RoleGroupID),
(r => r.RoleGroupID),
((u, r) => new { userInRoleGroup = u, roleInRoleGroups = r }));
var rrgOfRolesJoin = rolesOfRoleGroupJoin.Join(roles,
(r => r.roleInRoleGroups.RoleID),
(rs => rs.RoleID),
((r, rs) => new { rolesOfRoleGroupJoin = r, roles = rs }));
var roleServiceJoin = rrgOfRolesJoin.Join(serviceOfRoles,
(rrg => rrg.roles.RoleID),
(sor => sor.RoleID),
((rrg, sor) => new { rrgOfRolesJoin = rrg, serviceOfRoles = sor }));
var serviceOfRolesServiceJoin = roleServiceJoin.Join(service,
(rs => rs.serviceOfRoles.ServiceID),
(s => s.ServiceID),
((rs, s) => new { roleServiceJoin = rs, service = s }));
var serviceActionJoin = serviceOfRolesServiceJoin.Join(serviceOfAction,
(sors => sors.service.ServiceID),
(soa => soa.ServiceID),
((sors, soa) => new { serviceOfRolesServiceJoin = sors, serviceOfAction = soa }));
var serviceActionWithActionJoin = serviceActionJoin.Join(actions,
(sa => sa.serviceOfAction.ActionProviderID),
(a => a.ActionProviderID),
((sa, a) => new { serviceActionJoin = sa, actions = a }));
var actionNames = serviceActionWithActionJoin.Where(s => s.actions.Description.Equals(actionName));
if (actionNames != null && actionNames.Count() > 0)
{
return true;
}
return false;
}
}
Thank you
I assume that with "`A method in C# which performs about 6 JOIN statements and returns a value." you are doing it in Linq-to-SQL or Linq-to-Entities that translates all the joins to one query that hits the database.
In that case, there will be no noticable differences. The query execution plan is cached in the same way for both SPs and dynamically generated queries (SPs used to have better performance, but that difference is removed since several years).
If you are doing the join "in memory" by retrieving separate results from the DB you will get much, much worse performance.

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