Pass property as parameter to create a filtering method with LINQ/C#?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Pass property as parameter to create a filtering method with LINQ/C#?



Suppose I have these classes:


private class Model

public DateTime FirstDate get; set;
public DateTime SecondDate get; set;
public DateTime FinalDate get; set;
public string Name get; set;

private class Mommasan : Bandana

public DateTime RestDate get; set;
public DateTime TurboDate get; set;
public string Name get; set;



All dates need to be filtered up to 3 days. So how can I create a generic filtering method for the DateTime properties? Using IQueryable? I was hoping it would end up something like this:


DateTime


IQueryable



(Obviously will not compile, but I'm guessing the idea)


void DoSpecificFilter<T>(ref IQueryable<T> query, DateTimeProperty property)

DateTime today = today;
query = query.Where(a => property <= today && today <= property.AddDays(3);



So if I need to filter Models who are on their final date, something like:


Models


DoSpecificFilter<Model>(ref alreadyFilteredQuery, a => a.FinalDate);



and Mommasans who are on rest:


Mommasans


DoSpecificFilter<Mommasan>(ref alreadyFilteredQuery, a => a.RestDate);



Is this kind of thing possible at all or am I having the entirely wrong idea? Thanks!




2 Answers
2



Why not write an extension method on DateTime, and not look at the class at all? Something like ...


public static class Extensions

public static IQueryable<T> DoSpecificFilter<T>(this DateTime value, IQueryable<T> query)

DateTime today = DateTime.Now;
IQueryable<T> ret = query.Where(a => value <= today && today <= value.AddDays(3));
return ret;






Will this work when used on Entity Framework?
– AwonDanag
Aug 10 at 9:38





I don't see why it wouldn't .. give it a bash?
– WynDiesel
Aug 10 at 9:39





I think it wouldn't, because I don't see any pointers to the property being filtered...
– AwonDanag
Aug 10 at 9:44



You could work with DateTime-Selectors. For multiple DateTime fields you would have to chain them together. It would probably be easier to write an extention method for this:


DateTime


DateTime


public static class ExtentionMethods

public static IQueryable<T> DoSpecificFilter<T>(
this IQueryable<T> query,
Expression<Func<T, DateTime>> dateSelector,
DateTime filterValue,
bool blnTopLimit)




Then you could use it like this:


var query = queryableCollection
.DoSpecificFilter((a) => a.RestDate, DateTime.Today, false)
.DoSpecificFilter((a) => a.TurboDate, DateTime.Today, true);





Intuition tells me this won't work, because LINQ doesn't accept Funcs, but Expressions. Changing the parameter type to Expression<Func<T, DateTime>> should work. You can still use it the same way with lambdas.
– V0ldek
Aug 10 at 9:35



Func


Expressions


Expression<Func<T, DateTime>>





@V0ldek Thanks for the hint. I changed it.
– DeveloperExceptionError
Aug 10 at 9:40





Hey thanks, but what's the final bool parameter for?
– AwonDanag
Aug 10 at 9:45


bool





Can the where T : bit be generic or a parameter as well?
– AwonDanag
Aug 10 at 9:49


where T :





@DeveloperExceptionError I think you're missing a <T> after the method name, and why does dateSelector(a) don't work?
– AwonDanag
Aug 10 at 10:04


<T>


dateSelector(a)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard