c# - New to programming how to make this code more concise -
hi first question apologies if basic - new programming!!! using c# in mvc trying select object has date property entitymodel context. date selects relevant weight object , on list of "set" objects.
the code works , want general guidance on how make code more concise. here code:
public actionresult showdiary(string datein) { localtestentities1 dblists = new localtestentities1(); datetime date = convert.todatetime(datein); ienumerable<exercisediary> diary = o in dblists.exercisediaries o.date == date select o; var mydiary = diary.tolist(); exercisediary thediary = mydiary[0]; iqueryable<weight> weights = o in dblists.weights o.diaryid == thediary.id select o; var selectedweight = weights.tolist(); weight weight = selectedweight[0]; ienumerable<set> sets = x in dblists.sets x.weightid == weight.weightid select x; return view(sets); }
it seems taking many steps here. know returning 1 object diary. there way object dblists without sending ienumerable?
there's many ways things, but... think easiest way use first()
since grabbing first result in list.
another way make little cleaner put linq statements on multiple lines did sets
.
you can use var
, people , others don't have compiler infer type. did sets
below. feel cleans code bit when have large declarations ienumerable
, generics.
public actionresult showdiary(string datein) { localtestentities1 dblists = new localtestentities1(); datetime date = convert.todatetime(datein); exercisediary thediary = dblists.exercisediaries.first(o => o.date == date); weight weight = dblists.weights.first(o.diaryid == thediary.id); var sets = x in dblists.sets x.weightid == weight.weightid select x; return view(sets); }
imo easier read had in answer above.
Comments
Post a Comment