Problematic behavior of Linq Union?
By : Mallik Reddy
Date : March 29 2020, 07:55 AM
wish helps you You are using wrong operation as other answer explaining. But still it is interesting why your code works incorrectly despite looking fine. let's modify your app a bit: code :
IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
IEnumerable<String> allLexicals = new List<String> { "test", "Test", "T", "t" };
IEnumerable<String> lexicals = new List<String>();
foreach (String s in lexicalStrings)
{
lexicals = lexicals.Union(
allLexicals.Where(
lexical =>
{
Console.WriteLine(s);
return lexical == s;
}
)
);
}
Console.WriteLine();
foreach (var item in lexicals)
{
}
t
t
t
t
t
t
t
t
IEnumerable<String> lexicalStrings = new List<String> { "test", "t" };
IEnumerable<String> allLexicals = new List<String> { "test", "Test", "T", "t" };
IEnumerable<String> lexicals = new List<String>();
foreach (String s in lexicalStrings)
{
string ls = s;
lexicals = lexicals.Union(
allLexicals.Where(
lexical =>
{
Console.WriteLine(ls);
return lexical == ls;
}
)
);
}
foreach (var item in lexicals)
{
}
test
test
test
test
t
t
t
t
|
IEnumerator: Is it normal to have an empty Dispose method?
By : user2071873
Date : March 29 2020, 07:55 AM
To fix this issue Yes, it is. IEnumerator implements IDisposable in case you make an enumerator that does need to be disposed. Since most enumerators don't need to be disposed, the method will usually be empty.
|
Different exception-throwing behavior of IEnumerator.Current and IEnumerator<T>.Current
By : Abdullahi Hassan
Date : March 29 2020, 07:55 AM
wish of those help The behavior of the generic enumerator is undefined, anything is possible and it is ultimately up to the collection type to define what undefined is going to mean. But they can do something reasonable beyond throwing, the generic enumerators know the type of the collection object. So they can return default(T).
|
Strange 'Dispose' Error when calling with 'Using' or directly calling 'DIspose'
By : user3036407
Date : March 29 2020, 07:55 AM
|
How to convert UNION ALL to OR in Linq to SQL expression trees (Oracle ORA-00932: inconsistent datatypes: expected got C
By : Eran Rashkes
Date : March 29 2020, 07:55 AM
Hope this helps Based on the additional information from the comments, the problematic queries are produced by the following procedure: code :
public static IQueryable<T> ApplySearch<T>(this IQueryable<T> queryable, SearchModel search) where T : class
{
var subQueries = new List<IQueryable<T>>();
if (search != null)
{
if (search.PolicyNumber.HasValue && typeof (IPolicyNumber).IsAssignableFrom(queryable.ElementType))
{
subQueries.Add(queryable.SearchByPolicyNumber(search));
}
if (search.UniqueId.HasValue && typeof (IUniqueId).IsAssignableFrom(queryable.ElementType))
{
subQueries.Add(queryable.SearchByUniqueId(search));
}
if (!string.IsNullOrWhiteSpace(search.PostCode) && typeof(IPostCode).IsAssignableFrom(queryable.ElementType))
{
subQueries.Add(queryable.SearchByPostCode(search));
}
}
return subQueries.DefaultIfEmpty(queryable)
.Aggregate((a, b) => a.Union(b));
}
public static IQueryable<T> SearchByPolicyNumber<T>(this IQueryable<T> queryable, SearchModel search) where T : class
{
return queryable.Where(x => predicate_using_PolicyNumber(x, search));
}
public static IQueryable<T> SearchByUniqueId<T>(this IQueryable<T> queryable, SearchModel search) where T : class
{
return queryable.Where(x => predicate_using_UniqueId(x, search));
}
public static IQueryable<T> SearchByPostCode<T>(this IQueryable<T> queryable, SearchModel search) where T : class
{
return queryable.Where(x => predicate_using_PostCode(x, search));
}
public static class SearchPredicates
{
public static Expression<Func<T, bool>> ByPolicyNumber<T>(SearchModel search) where T : class
{
return x => predicate_using_PolicyNumber(x, search);
}
public static Expression<Func<T, bool>> ByUniqueId<T>(SearchModel search) where T : class
{
return x => predicate_using_UniqueId(x, search);
}
public static Expression<Func<T, bool>> ByPostCode<T>(SearchModel search) where T : class
{
return x => predicate_using_PostCode(x, search);
}
}
public static IQueryable<T> ApplySearch<T>(this IQueryable<T> queryable, SearchModel search) where T : class
{
var predicates = new List<Expression<<Func<T, bool>>>();
if (search != null)
{
if (search.PolicyNumber.HasValue && typeof (IPolicyNumber).IsAssignableFrom(queryable.ElementType))
predicates.Add(SearchPredicates.ByPolicyNumber(search));
if (search.UniqueId.HasValue && typeof (IUniqueId).IsAssignableFrom(queryable.ElementType))
predicates.Add(SearchPredicates.ByUniqueId(search));
if (!string.IsNullOrWhiteSpace(search.PostCode) && typeof(IPostCode).IsAssignableFrom(queryable.ElementType))
predicates.Add(SearchPredicates.ByPostCode(search));
}
if (predicates.Count == 0)
return queryable;
var parameter = predicates[0].Parameters[0];
var condition = predicates[0].Body;
for (int i = 1; i < predicates.Count; i++)
condition = Expression.Or(condition, predicates[i].Body.ReplaceParameter(predicates[i].Parameters[0], parameter));
var predicate = Expression.Lambda<Func<T, bool>>(condition, parameter);
return queryable.Where(predicate);
}
public static class ExpressionUtils
{
public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target)
{
return new ParameterReplacer { Source = source, Target = target }.Visit(expression);
}
class ParameterReplacer : ExpressionVisitor
{
public ParameterExpression Source;
public Expression Target;
protected override Expression VisitParameter(ParameterExpression node)
{
return node == Source ? Target : base.VisitParameter(node);
}
}
}
|