En expresiones .net (C# o vb), cómo implementaría la práctica funcionalidad IN() de SQL?¿Cómo se implementa el equivalente de SQL IN() usando .net
es decir, valor en (1, 2, 4, 7)
en lugar de:
valor = 1 o el valor = 2 o el valor = 4 o el valor = 7
En expresiones .net (C# o vb), cómo implementaría la práctica funcionalidad IN() de SQL?¿Cómo se implementa el equivalente de SQL IN() usando .net
es decir, valor en (1, 2, 4, 7)
en lugar de:
valor = 1 o el valor = 2 o el valor = 4 o el valor = 7
using System;
using System.Linq;
static class SqlStyleExtensions
{
public static bool In(this string me, params string[] set)
{
return set.Contains(me);
}
}
Uso :
if (Variable.In("AC", "BC", "EA"))
{
}
También podría hacer que la cadena sea un parámetro genérico ... – ngoozeff
params hace que la llamada sea realmente clara. –
Versión genérica - bool estático público En
if((new int[] {1, 2, 4, 7}).Contains(value))
{
// Do some work.
}
Como otros han señalado, y ou podría crear un método de extensión en() (lo mantendré genérico para que pueda usarlo en cualquier tipo):
public static bool In<T>(T this obj, IEnumerable<T> col)
{
return col.Contains(obj);
}
Así el ejemplo inicial se convierte en:
if(value.In(new int[] {1, 2, 4, 7}))
{
// Do some work.
}
he hecho una método de extensión para esto que me parece bastante útil. Sin embargo, no es mucho más que azúcar sintáctica que envuelve la función IEnumerable.Contains() existente.
/// <summary>
/// Returns true if the value is represented in the provided enumeration.
/// </summary>
/// <typeparam name="T">Type of the value</typeparam>
/// <param name="obj">The object to check if the enumeration contains</param>
/// <param name="values">The enumeration that might contain the object</param>
/// <returns>True if the object exists in the enumeration</returns>
public static bool In<T>(this T obj, IEnumerable<T> values) {
return values.Contains(obj);
}
Editar: Alguien se me adelantó, maldita sea. Me quedaré aquí por correo porque es una versión más genérica.
wasatz - publiqué una versión a continuación que se extendió a la expresión completa y la funcionalidad lambda. podría ser bueno probarlo como una alternativa si está usando IQueryables –
Puede usar el método Contiene() en la lista.
int myValue = 1;
List<int> checkValues = new List<int> { 1, 2, 3 };
if (checkValues.Contains(myValue))
// Do something
mediante LINQ
var q = from x in collection
where (new int[] { 1, 2, 4, 7}).Contains(x.value)
select x
Si va a hacer muchas búsquedas en el mismo conjunto de datos que es bueno desde la perspectiva del rendimiento de usar HashSet<T>
.
HashSet<int> numbers = new HashSet<int> { 1, 2, 4, 7 };
bool is5inSet = numbers.Contains(5);
Sé que hay CARGAS de respuestas aquí, pero esta es mi opinión sobre el tema, que se utiliza a diario en SubSonic. es un método de extensión:
public static IQueryable<T> WhereIn<T, TValue>(
this IQueryable<T> query,
Expression<Func<T, TValue>> selector,
params TValue[] collection) where T : class
{
if (selector == null) throw new ArgumentNullException("selector");
if (collection == null) throw new ArgumentNullException("collection");
ParameterExpression p = selector.Parameters.Single();
if (!collection.Any()) return query;
IEnumerable<Expression> equals = collection.Select(value =>
(Expression)Expression.Equal(selector.Body,
Expression.Constant(value, typeof(TValue))));
Expression body = equals.Aggregate(Expression.Or);
return query.Where(Expression.Lambda<Func<T, bool>>(body, p));
}
y WhereNotIn:
public static IQueryable<T> WhereNotIn<T, TValue>(
this IQueryable<T> query,
Expression<Func<T, TValue>> selector,
params TValue[] collection) where T : class
{
if (selector == null) throw new ArgumentNullException("selector");
if (collection == null) throw new ArgumentNullException("collection");
ParameterExpression p = selector.Parameters.Single();
if (!collection.Any()) return query;
IEnumerable<Expression> equals = collection.Select(value =>
(Expression)Expression.NotEqual(selector.Body,
Expression.Constant(value, typeof(TValue))));
Expression body = equals.Aggregate(Expression.And);
return query.Where(Expression.Lambda<Func<T, bool>>(body, p));
}
uso:
var args = new [] { 1, 2, 3 };
var bookings = _repository.Find(r => r.id > 0).WhereIn(x => x.BookingTypeID, args);
// OR we could just as easily plug args in as 1,2,3 as it's defined as params
var bookings2 = _repository.Find(r => r.id > 0).WhereIn(x => x.BookingTypeID, 1,2,3,90);
var bookings3 = _repository.Find(r => r.id > 0).WhereNotIn(x => x.BookingTypeID, 20,30,60);
esto realmente me hace sonreír cada vez que revisarlo :)
Jim
[editar] - procedentes originalmente de aquí en SO pero modificado para utilizar params IQueryable y: 'Contains()' workaround using Linq to Entities?
+1 ¡Muy bien! – wasatz
O usando System.Linq
...
(VB.NET)
Enumerable.Contains({1, 2, 4, 7}, value)
o
{1, 2, 4, 7}.Contains(value)
(C#)
Enumerable.Contains(new int[]{1, 2, 4, 7}, value);
o
new int[] {1, 2, 4, 7}.Contains(value);
Simplemente esto: '{1, 2, 4, 7} .Contains (value)'. – Neolisk
He aquí algunos simples LINQ con un poco de pseudo código. No hay necesidad de reinventar la rueda.
int[] values = new int[]{1, 2, 4, 7};
int target = 2;
bool contains = values.Any(v => v == target);
O use .Contains
como algunos han sugerido.
¿qué versión de .NET framework? –
Uso principalmente 3.5. ¿No estoy seguro de si hay un anthing en 4.0 que lo hace más fácil? –