How to use UnitOfWOrk .GetByCondition() - c#

i have a generic repository
public TEntity GetByCondition(Func<TEntity, Boolean> where)
{
return DbSet.Where(where).FirstOrDefault<TEntity>();
}
i need to get the selected records based on condition;
public IEnumerable<ResultEntity> GetResultByParams(string _RollNo, string _Class)
{
var result = _unitOfWork.ResultRepository.GetByCondition(); // i need to check ondition of _RollNo here. Please guide me the usage.
}
// how to use the var result = _unitOfWork.ResultRepository.GetByCondition(); with condition

In general:
var result = _unitOfWork.ResultRepository.GetByCondition(res => /*your condition for "result" here*/);
By condition I mean expression that returns bool.
For example:
var result = _unitOfWork.ResultRepository.GetByCondition(res => res.RollNo == _RollNo);
It basically means: "give me all Results which have RollNo equals to _RollNo"
I recommend you to read explanations of Func from following question, it may be useful: Explanation of Func.
And, of course, msdn reference for the type of Func-s used in your case.

Related

Add a where clause to IQueryable without using generics

I'm trying to write a piece of code that is going to be used to select a value from a database. As a test I have already created this test version of the method that "works" but isn't great.
private object? FindObjectByProperty(IQueryable query, PropertyInfo primaryKeyProperty, object lookupValue)
{
object? result = null;
foreach (var value in query)
{
if (!primaryKeyProperty.GetValue(value)!.Equals(lookupValue))
continue;
result = value;
break;
}
return result;
}
The code will iterate through the query and find the first entry that matches the Equals. However, if the query contains millions of rows, as it's likely to when the IQueryable is really a DbSet (Which in this use case it is. Only I don't know what T, as it could be any of the DbSets on my EF Core data context.
What I'd like to end up with is something that looks something along these lines....
private object? FindObjectByProperty(IQueryable query, PropertyInfo primaryKeyProperty, object lookupValue)
{
return query.Where( x => x.*primaryKeyProperty* == lookupValue).FirstOrDefault();
}
The above code is pseudo code, the problems that I have is that query does not have a .Where available. Also primaryKeyProperty will need to be translated to the actual property.
The idea is that when this code is executed, ultimately executing the query, will generate a sql statement which selects a single item and returns it.
Can anyone help with solving this?
Update:
I'm working on a solution to this, so far this is what I've come up with
//using System.Linq.Dynamic.Core;
private object? FindObjectByProperty(IQueryable query, PropertyInfo primaryKeyProperty, object lookupValue)
{
var parameter = Expression.Parameter(query.ElementType);
var e1 = Expression.Equal(Expression.Property(parameter, primaryKeyProperty.Name), Expression.Constant(lookupValue));
var lambda = Expression.Lambda<Func<object, bool>>(e1, parameter);
return query.Where(lambda).FirstOrDefault();
}
This is failing on the because the func uses Object, when it really needs the real type. Trying to figure that bit out. This is getting closer.
Update #2:
Here's the answer that I needed
private object? FindObjectByProperty(IQueryable query, PropertyInfo primaryKeyProperty, object lookupValue)
{
var parameter = Expression.Parameter(query.ElementType);
var propExpr = Expression.Property(parameter, primaryKeyProperty);
var lambdaBody = Expression.Equal(propExpr, Expression.Constant(lookupValue, primaryKeyProperty.PropertyType));
var filterEFn = Expression.Lambda(lambdaBody, parameter);
return query.Where(filterEFn).FirstOrDefault();
}
The difference is that the Expression.Lambda no longer tries to define the func using generics. This is the only change that I needed to do to make the code function as I wanted.
In the test case that I was using, the T-SQL produced to lookup the value looks like this...
SELECT TOP(1) [g].[Id], [g].[Deleted], [g].[Guid], [g].[Name], [g].[ParentId]
FROM [Glossaries].[Glossaries] AS [g]
WHERE [g].[Id] = CAST(3 AS bigint)
The table [Glossaries].[Glossaries] is provided by the input query. The column name Id is provided by the primaryKeyProperty, and the number 3 is provided by the lookupValue.
This is perfect for my needs as I simply needed to select that one row and nothing else, so that I can effectively lazy load the my object property when I need it, and not before.
Also this code will be reused for many different tables.
In order to build add Where to an IQueryable where you don't have access to the actual IQueryable<T>, you need to take a step back (or up?) from calling Where at compile-time, and build the Where call at runtime, as well as the predicate lambda:
public static class DBExt {
public static IQueryable WherePropertyIs<T2>(this IQueryable src, PropertyInfo propInfo, T2 propValue) {
// return src.Where(s => s.{propInfo} == propValue)
// (T s)
var sParam = Expression.Parameter(src.ElementType, "s");
// s.propInfo
var propExpr = Expression.Property(sParam, propInfo);
// s.{propInfo} == propValue
var lambdaBody = Expression.Equal(propExpr, Expression.Constant(propValue));
// (T s) => s.{PropInfo} == propValue
var filterEFn = Expression.Lambda(lambdaBody, sParam);
var origQuery = src.Expression;
// IQueryable<Tx>.Where<Tx, Expression<Func<Tx, bool>>>()
var whereGenericMI = typeof(Queryable).GetMethods("Where", 2).Where(mi => mi.GetParameters()[1].ParameterType.GenericTypeArguments[0].GenericTypeArguments.Length == 2).First();
// IQueryable<T>.Where<T, Expression<Func<T, bool>>>()
var whereMI = whereGenericMI.MakeGenericMethod(src.ElementType);
// src.Where(s => s.{propertyInfo} == propValue)
var newQuery = Expression.Call(whereMI, origQuery, filterEFn);
return src.Provider.CreateQuery(newQuery);
}
}

Dynamically change query type in NHibernate

I want to achieve that NHibernate queries different types based on a condition. Let's say the condition is false then the query should look like this:
var myVar = session.Query<TypeWhenFalse>()
.Where(a => condition)
And when the condition is true it should look like this:
var myVar = session.Query<TypeWhenTrue>()
.Where(a => condition)
Notice the different query types TypeWhenFalse and TypeWhenTrue. Because of duplicate code, I don't wanna use a if clause to set the different types.
Is there a way to do this dynamically in order to avoid an if clause? Couldn't find anything helpful in the internet.
Thanks in advance :-)
You have to use generic type method and you can use an extension to implement what you want.
But.. in order to have this to make sense, you need to have both returned types to have common structures implemented by an interface.
See this example:
public static void Main()
{
var abc = new List<long>(){ 1,2,3,4};
var result = abc.AsQueryable().ConvertExtension();
}
public static IQueryable ConvertExtension<T>(this IQueryable<T> source, bool condition=false)
{
return condition
? (IQueryable) source.Select(x => x.ToString()).AsQueryable<string>()
: source.Select(x => int.Parse(x.ToString())).AsQueryable<int>();
}
More about Generic Types: C# generic types
More about Interfaces: Interfaces
If you implement an interface for both types, you can type your resulting Iqueryable as the interface. The method would be IQueryable ConvertExtension
And you would be able to manipulate both Types as it.
The code you provided is very less to suggest anything confirmed.
You can use generics to achieve this easily. I mean, make this function generic and pass the generic type from outside. This should resolve your concerns.
public yourtype GetData<T>()
{
var myVar = session.Query<T>()
.Where(a => condition);
return myVar;//or whatever
}
and your calling code:
if(false)
myVar = GetData<TypeWhenFalse>();
else
myVar = GetData<TypeWhenTrue>();
As you said, you do not want to use if...else to avoid redundant code. If you are agreed for little redundancy, consider following solution:
var query;
if(false)
query = session.Query<TypeWhenFalse>();
else
query = session.Query<TypeWhenTrue>();
var myVar = query.Where(a => condition);

Cannot resolve method between Enumerable and Queryable candidates

I have this method in a class called Invoice:
public static Expression<Func<Invoice, bool>> IsAllocated()
{
return i => i.TotalAmountDue == i.GetAllocationsTotal();
}
I have a list like this:
IQueryable<Invoice> invoices
And I need to filter it like that (it's Linq to Entity):
var filteredInvoices = invoices.Where(i => Invoice.IsAllocated());
In this line I'm getting two errors:
Cannot resolve method ... candidates are .... one in Enumerable and the other on in Queryable.
And also:
Cannot convert expression type Expression<Func<Invoice,bool>> to
return type 'bool'
I've tried a lot of things I've found in SO with no luck. Can someone say me what is missing here or at least, which one of the two errors is at the root of the problem?
Your method returns an appropriate expression tree already - you just need to call it, not call it in a lambda expression:
var filteredInvoices = invoices.Where(Invoice.IsAllocated());
Expression are representation and not delegate by themselves. You should create a delegate out of it first
static Expression<Func<Invoice, bool>> IsAllocatedExpr()
{
return i => i.TotalAmountDue == i.GetAllocationsTotal();
}
public static Func<Invoice, bool> IsAllocated = IsAllocatedExpr().Compile();
and then
var filteredInvoices = invoices.Where(i => Invoice.IsAllocated(i));

PredicateBuilder Returns No Rows

This code correctly returns one row:
_loadedAssemblies.ForEach(x =>
{
foundTypes.AddRange(from t in x.GetTypes()
where t.GetInterfaces().Contains(typeof(TInterface))
&& t.BaseType.Name.LeftOf('`') == baseClass.Name.LeftOf('`')
select t);
}
However, when I use PredicateBuilder, I get zero rows:
var compiledPredicate = CompiledPredicate<TInterface>();
_loadedAssemblies.ForEach(x =>
{
foundTypes.AddRange(from t in x.GetTypes()
where compiledPredicate.Invoke(typeof(TInterface))
select t);
}
private static Func<Type, bool> CompiledPredicate<T>() where T : class
{
// True means all records will be returned if no other predicates are applied.
var predicate = PredicateBuilder.True<Type>();
// Get types that implement the interface (T).
predicate = predicate.And(t => t.GetInterfaces().Contains(typeof(T)));
// If the config file includes filtering by base class, then filter by it.
if (!string.IsNullOrWhiteSpace(_baseClass))
{
Type baseClass = Type.GetType(_baseClass);
predicate = predicate.And(t => t.BaseType.Name.LeftOf('`') == baseClass.Name.LeftOf('`'));
}
return predicate.Compile();
}
Someone suggested creating a copy of my loop variable, but I tried that and I still get zero rows. I'm not sure why using PredicateBuilder returns no rows. Any idea what I'm missing?
The change that you mentioned in comments (foundTypes.AddRange(x.GetTypes().AsQueryable().Where(compiledPredicate));) had nothing at all to do with the fact that you were using AsQueryable. In the first case you're passing in a hard coded type to each call of your predicate, in the second you're passing the given item from the sequence. Had you removed the AsQueryable and used Enumerable.Where it would also work, or had you passed the current item, rather than a hard coded type, when invoking it that would also work.
So you can simply do:
foundTypes.AddRange(x.GetTypes().Where(compiledPredicate));
Also, when creating the predicate, there's no need to do as much work as you're doing. Expressions take a fair amount of extra work to deal with. With linq to objects you only need to deal with delegates, which are much less finicky.
private static Func<Type, bool> CompiledPredicate<T>() where T : class
{
Func<Type, bool> predicate = t => t.GetInterfaces().Contains(typeof(T));
if (!string.IsNullOrWhiteSpace(_baseClass))
{
Type baseClass = Type.GetType(_baseClass);
return t => predicate(t) &&
t.BaseType.Name.LeftOf('`') == baseClass.Name.LeftOf('`');
}
return predicate;
}

Invoke a C# inner Expression with a member property of a parameter to an outer expression

I am using the Albaharis PredicateBuilder as found here http://www.albahari.com/nutshell/predicatebuilder.aspx to filter results in a Linq-to-SQL application. This has been working great.
What I am trying to do now, is reuse the existing filtering predicate expression to filter an object that has the existing filtered object as a property.
For example, I have 2 classes, Order and Customer. I already have a method that returns a Expression<Func<Customer, bool>>, which is built using the above mentioned predicate builder. I now want to reuse this in my Order filtering method, which will return a Expression<Func<Customer, bool>> by somehow passing the Order.Customer property (expression?) into my Customer filter method.
I have something like this (far from complete, but I hope you get the idea):
public class CustomerSearchCriteria
{
public Expression<Func<Customer, bool>> FilterPredicate()
{
// Start with predicate to include everything
var result = PredicateBuilder.True<Customer>();
// Build predicate from criteria
if (!String.IsNullOrEmpty(this.Name))
{
result = result.And(c => SqlMethods.Like(c.Name, this.Name));
}
// etc. etc. etc
}
public class OrderSearchCriteria
{
public Expression<Func<Order, bool>> FilterPredicate()
{
// Start with predicate to include everything
var result = PredicateBuilder.True<Order>();
// Build predicate from criteria
if (!String.IsNullOrEmpty(this.Reference))
{
result = result.And(o => SqlMethods.Like(o.Reference, this.Reference));
}
// etc. etc. etc
// This is where I would like to do something like:
// result = result.And(o => o.Customer "matches" this.CustomerCriteria.FilterPredicate()
}
Can any Linq expression guru help me?
Thanks in advance.
If you use the Albaharis' LinqKit, you should be able to do something like this:
var customerFilter = this.CustomerCriteria.FilterPredicate();
// create an expression that shows us invoking the filter on o.Customer
Expression<Func<Order, bool>> customerOrderFilter =
o => customerFilter.Invoke(o.Customer);
// "Expand" the expression: this creates a new expression tree
// where the "Invoke" is replaced by the actual predicate.
result = result.And(customerOrderFilter.Expand())

Categories