Struggling a bit today.
I have the following method that returns a list of products..lovely.
public static List<tblWeight> GetProductInfo(string memberid, string locationid, string basematerial, string source)
{
MyEntities getproductinfo = new MyEntities ();
return (from p in getproductinfo .tblWeights
where p.MemberId == memberid &&
p.LocationId == locationid &&
p.BaseMaterialName == basematerial &&
p.WeightStatus == source
select p)
.ToList();
Where basematerial & source are drop down lists.
How do I go about incorporating a few IF statements into the where clause?
For example, if the basematerial ddl is not touched but an item in the source ddl is selected, the result would return everything associated with basematerial but filtered by the selected source.
Does that even make sense?!
I'm not even sure I am taking the correct approach - please forgive my ignorance.
you can add them to your query on need:
var r = (from p in getproductinfo .tblWeights
where p.MemberId == memberid &&
p.LocationId == locationid &&
p.WeightStatus == source
select p)
if (!String.IsNullOrEmpty(basematrial))
r = r.Where(p => p.BaseMaterialName == basematerial);
return r.ToList();
Consider implementing these extension methods named WhereIf.
You pass it two parameters: a statement evaluated to a boolean, and a lambda function. If the bool statement evaluates to true, the lambda is added.
WhereIf on ExtensionMethod.net
Your query could look like:
return getproductinfo.tblWeights
.Where(w=> w.MemberId == memberid &&
w.LocationId == locationid)
.WhereIf(!string.IsNullOrEmpty(basematerial), w=>w.BaseMaterialName == basematerial)
.WhereIf(!string.IsNullOrEmpty(source), w=>w.WeightStatus == source)
.ToList();
Here they are, for both IEnumerable and IQueryable. This allows you to use .WhereIf() in LINQ To SQL, Entity Framework, Lists, Arrays, and anything else that implements those 2 interfaces.
public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
{
if (condition)
return source.Where(predicate);
else
return source;
}
public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, int, bool> predicate)
{
if (condition)
return source.Where(predicate);
else
return source;
}
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Func<TSource, bool> predicate)
{
if (condition)
return source.Where(predicate);
else
return source;
}
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, bool condition, Func<TSource, int, bool> predicate)
{
if (condition)
return source.Where(predicate);
else
return source;
}
Related
I have this function:
public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> exp1, Expression<Func<TSource, bool>> exp2, Expression<Func<TSource, bool>> exp3)
{
}
and i use the function like this :
adverts.WhereIf(x=>x.DeactivatedDate.HasValue, x => x.DeactivatedDate.Value > starDateTime, x => x.ModifiedDate > starDateTime);
How can i get the value of exp1 ?
In the extenssion i need to know if DeactivatedDate.HasValue is true
You can evaluate the expression, but you need an instance of TSource to evaluate against. Since your "input" is a collection of TSource youe solution might be something like:
foreach(TSource t in source)
{
bool isTrue = exp1.Compile()(t); // evaluate exp1 for this item
if(isTrue)
// do something
else
// do something else
}
but note that your return type in an IQueryable<TSource> - so I'm not sure you've fully thought through how you intend to build up the resulting query...
I use this predicate with EF and lamdba expression :
public class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
}
public static class ExpressionExtensions
{
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.And);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.Or);
}
}
Now I just want to dynamically build this query :
Query(ufe => (ufe.FilmEtat.filmetat_code == etatString && ufe.user_id == 2) || (ufe.FilmEtat.filmetat_code == etatString && ufe.user_id == 11)).ToList();
I already tried :
var predicate = PredicateBuilder.True<UtilisateurFilmEtat>();
int i = 0;
foreach (int utilisateurId in listUtilisateurId)
{
if (i == 0)
predicate = ufe => (ufe.FilmEtat.filmetat_code == etatString && ufe.user_id == utilisateurId);
else
predicate.Or(ufe => ufe.FilmEtat.filmetat_code == etatString && ufe.user_id == utilisateurId);
i++;
}
The query is working but not return the good results...
I am becoming crazy :(
Need your help.
Thank you
Your question seems very similar to this one: Linq to SQL how to do "where [column] in (list of values)", although not an exact duplicate.
I can see that you're trying to dynamically build a query by combining other querys with ||, which is what you'd want to do if you only had a comparison operator...
Instead, how about something like this: ufe => listUtilisateurId.Contains(ufe.user_id)
I'd like to split a sequence in C# to a sequence of sequences using LINQ. I've done some investigation, and the closest SO article I've found that is slightly related is this.
However, this question only asks how to partition the original sequence based upon a constant value. I would like to partition my sequence based on an operation.
Specifically, I have a list of objects which contain a decimal property.
public class ExampleClass
{
public decimal TheValue { get; set; }
}
Let's say I have a sequence of ExampleClass, and the corresponding sequence of values of TheValue is:
{0,1,2,3,1,1,4,6,7,0,1,0,2,3,5,7,6,5,4,3,2,1}
I'd like to partition the original sequence into an IEnumerable<IEnumerable<ExampleClass>> with values of TheValue resembling:
{{0,1,2,3}, {1,1,4,6,7}, {0,1}, {0,2,3,5,7}, {6,5,4,3,2,1}}
I'm just lost on how this would be implemented. SO, can you help?
I have a seriously ugly solution right now, but have a "feeling" that LINQ will increase the elegance of my code.
Okay, I think we can do this...
public static IEnumerable<IEnumerable<TElement>>
PartitionMontonically<TElement, TKey>
(this IEnumerable<TElement> source,
Func<TElement, TKey> selector)
{
// TODO: Argument validation and custom comparisons
Comparer<TKey> keyComparer = Comparer<TKey>.Default;
using (var iterator = source.GetEnumerator())
{
if (!iterator.MoveNext())
{
yield break;
}
TKey currentKey = selector(iterator.Current);
List<TElement> currentList = new List<TElement> { iterator.Current };
int sign = 0;
while (iterator.MoveNext())
{
TElement element = iterator.Current;
TKey key = selector(element);
int nextSign = Math.Sign(keyComparer.Compare(currentKey, key));
// Haven't decided a direction yet
if (sign == 0)
{
sign = nextSign;
currentList.Add(element);
}
// Same direction or no change
else if (sign == nextSign || nextSign == 0)
{
currentList.Add(element);
}
else // Change in direction: yield current list and start a new one
{
yield return currentList;
currentList = new List<TElement> { element };
sign = 0;
}
currentKey = key;
}
yield return currentList;
}
}
Completely untested, but I think it might work...
alternatively with linq operators and some abuse of .net closures by reference.
public static IEnumerable<IEnumerable<T>> Monotonic<T>(this IEnumerable<T> enumerable)
{
var comparator = Comparer<T>.Default;
int i = 0;
T last = default(T);
return enumerable.GroupBy((value) => { i = comparator.Compare(value, last) > 0 ? i : i+1; last = value; return i; }).Select((group) => group.Select((_) => _));
}
Taken from some random utility code for partitioning IEnumerable's into a makeshift table for logging. If I recall properly, the odd ending Select is to prevent ambiguity when the input is an enumeration of strings.
Here's a custom LINQ operator which splits a sequence according to just about any criteria. Its parameters are:
xs: the input element sequence.
func: a function which accepts the "current" input element and a state object, and returns as a tuple:
a bool stating whether the input sequence should be split before the "current" element; and
a state object which will be passed to the next invocation of func.
initialState: the state object that gets passed to func on its first invocation.
Here it is, along with a helper class (required because yield return apparently cannot be nested):
public static IEnumerable<IEnumerable<T>> Split<T, TState>(
this IEnumerable<T> xs,
Func<T, TState, Tuple<bool, TState>> func,
TState initialState)
{
using (var splitter = new Splitter<T, TState>(xs, func, initialState))
{
while (splitter.HasNext)
{
yield return splitter.GetNext();
}
}
}
internal sealed class Splitter<T, TState> : IDisposable
{
public Splitter(IEnumerable<T> xs,
Func<T, TState, Tuple<bool, TState>> func,
TState initialState)
{
this.xs = xs.GetEnumerator();
this.func = func;
this.state = initialState;
this.hasNext = this.xs.MoveNext();
}
private readonly IEnumerator<T> xs;
private readonly Func<T, TState, Tuple<bool, TState>> func;
private bool hasNext;
private TState state;
public bool HasNext { get { return hasNext; } }
public IEnumerable<T> GetNext()
{
while (hasNext)
{
Tuple<bool, TState> decision = func(xs.Current, state);
state = decision.Item2;
if (decision.Item1) yield break;
yield return xs.Current;
hasNext = xs.MoveNext();
}
}
public void Dispose() { xs.Dispose(); }
}
Note: Here are some of the design decisions that went into the Split method:
It should make only a single pass over the sequence.
State is made explicit so that it's possible to keep side effects out of func.
I was looking for a Linq RemoveFirst(Predicate<T> match) but could only find RemoveAll.
I know I can write my own extension method but was wondering if there already exists an equivalent function with a different name, or an easy way to achieve the same result.
Like this:
list.RemoveAt(list.FindIndex(x => thingy));
If no item is found, an exception will be thrown.
Note that this has nothing to do with LINQ and can only be done with List<T>.
This code does not actually "remove" the element from the sequence since, as #SLaks points out linq sequences are readonly, but it does skip the first occurrence of the element that meets the criteria. It is not particularly efficient as each list operation is going to iterate over the list. It is reasonably expressive of what you are trying to accomplish. Depending on how many items you expect to have in your list, this might be reasonable for you.
IEnumerable<int> x = Enumerable.Range(0, 20);
var y = x.TakeWhile(xx => xx < 10).Concat(x.SkipWhile(xx => xx < 10).Skip(1));
//Will write 0 1 2 ... 19, skipping 10
foreach(int a in y)
{
System.Diagnostics.Debug.WriteLine(a);
}
Since no one else has provided one, here's my extension method/enumerable that implements RemoveFirst(Predicate match). The trick is basically that you need to define your own IEnumerable to track the state properly--I couldn't find an easy way around that.
You can try it out in a .NET Fiddle here.
public static IEnumerable<T> RemoveFirst<T>(this IEnumerable<T> list, Func<T, bool> predicate)
{
return new RemoveFirstEnumerable<T>(list, predicate);
}
public static IEnumerable<T> RemoveFirst<T>(this IEnumerable<T> list, T item)
{
return RemoveFirst(list, x => Object.Equals(x, item));
}
private class RemoveFirstEnumerable<T> : IEnumerable<T>
{
IEnumerable<T> m_Source;
Func<T, bool> m_Predicate;
public RemoveFirstEnumerable(IEnumerable<T> source, Func<T, bool> predicate)
{
m_Source = source;
m_Predicate = predicate;
}
public IEnumerator<T> GetEnumerator()
{
return new RemoveFirstEnumerator(m_Source, m_Predicate);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new RemoveFirstEnumerator(m_Source, m_Predicate);
}
private class RemoveFirstEnumerator : IEnumerator<T>
{
IEnumerator<T> m_Enumerator;
Func<T, bool> m_Predicate;
bool m_RemovedOnce = false;
public RemoveFirstEnumerator(IEnumerable<T> source, Func<T, bool> predicate)
{
m_Enumerator = source.Where(WherePredicate).GetEnumerator();
m_Predicate = predicate;
}
bool WherePredicate(T current)
{
// terse version:
return m_RemovedOnce || !(m_RemovedOnce = m_Predicate(current));
// Long version
//if (m_RemovedOnce)
//{
// return true;
//}
//else
//{
// m_RemovedOnce = Object.Equals(x, item);
// return !m_RemovedOnce;
//}
}
public T Current
{
get { return m_Enumerator.Current; }
}
public bool MoveNext()
{
return m_Enumerator.MoveNext();
}
public void Reset()
{
m_Enumerator.Reset();
}
public void Dispose()
{
m_Enumerator.Dispose();
}
object IEnumerator.Current
{
get { return m_Enumerator.Current; }
}
}
}
Indeed the classic List has a RemoveAll(match) but not a RemoveFirst(match). An extension method could be
public static class ListExtensions
{
public static void RemoveFirst<T>(this List<T> list, Predicate<T> match)
{
list.RemoveAt(list.FindIndex(match));
}
}
SingleOrDefault returns null, but what if I want to assign values to represent the object that wasn't found?
you can do something like
myStrings.DefaultIfEmpty("myDefaultString").Single()
check out here
?? operator. If the left argument is null, evaluate and return the second argument.
myCollection.SingleOrDefault() ?? new[]{new Item(...)}
This will only work with reference types (or nullables), but it would do what you're looking for very simply.
You could roll your own.
public static T SingleOrDefault<T>(this IEnumerable<T> enumerable, T defaultValue) {
if ( 1 != enumerable.Count() ) {
return defaultValue;
}
return enumerable.Single();
}
This can be a bit expensive though because Count() requires you to process the entire collection and can be fairly expensive to run. It would be better to either call Single, catch the InvalidOperationException or roll a IsSingle method
public static bool IsSingle<T>(this IEnumerable<T> enumerable) {
using ( var e = enumerable.GetEnumerator() ) {
return e.MoveNext() && !e.MoveNext();
}
}
public static T SingleOrDefault<T>(this IEnumerable<T> enumerable, T defaultValue) {
if ( !enumerable.IsSingle() ) {
if( enumerable.IsEmpty() ) {
return defaultValue;
}
throw new InvalidOperationException("More than one element");
}
return enumerable.Single();
}
You could create your own extension methods -- SingleOrNew.
public static class IEnumerableExtensions
{
public static T SingleOrNew<T>( this IEnumerable<T> enumeration, T newValue )
{
T elem = enumeration.SingleOrDefault();
if (elem == null)
{
return newValue;
}
return elem;
}
public static T SingleOrNew<T>( this IEnumerable<T> enumeration, Func<T,bool> predicate, T newValue )
{
T elem = enumeration.SingleOrDefault( predicate );
if (elem == null)
{
return newValue;
}
return elem;
}
}