I have the following 2 methods that i thing could be compressed in one method using generics. What i have tried can not compile. Can someone let me know how this can be done? I need to check that 2 differents field of the table AgeLengths have at least one values.
Str_table has one to many relationship with AgeLengths.
public static bool HasMeanWeight(int id)
{
MyDataContext dc = new MyDataContext ();
return (from s in dc.Str_table
where s.SId == id
select s.AgeLengths
.Where(a => a.MeanWeight != null ).Any() == true
).FirstOrDefault();
}
public static bool HasNumbersData(int id)
{
MyDataContext dc = new MyDataContext ();
return (from s in dc.Str_table
where s.sId == id
select s.AgeLengths
.Where(a => a.Numbers != null).Any() == true
).FirstOrDefault();
}
Thanks in advance
B
Update: Apologies, I didn’t realise this was linq to sql. Dennis’s answer appears to be on the mark.
Try injecting a
Func<T, TResult>to inject the differing code:And call like so:
If the
NumbersandMeanWeightproperties are in the same inheritance hierarchy, then you can substituteobjectwith something more useful, but in this instanceobjectis fine as you are just testing for null.