C# 如何在C中向LINQ查询添加if语句?
请考虑以下查询方法:C# 如何在C中向LINQ查询添加if语句?,c#,linq,C#,Linq,请考虑以下查询方法: internal List<Product> productInStockQuery(InStock inStock1) { var inStock = from p in products where p.INStock == inStock1 select p; return inStock.ToList<Product>(); } 我希望该方法可以执行以下操作: 如果inSto
internal List<Product> productInStockQuery(InStock inStock1)
{
var inStock =
from p in products
where p.INStock == inStock1
select p;
return inStock.ToList<Product>();
}
我希望该方法可以执行以下操作:
如果inStock1==InStock.needToOrder,我希望它同时搜索以下两种内容:
instock1==InStock.needToOrder&&instock1==InStock.False
我该怎么做
我还想知道是否有可能创建一个方法,允许我在一个方法中搜索我的所有产品字段
编辑部分:我的第二个问题
试图更好地解释自己:我的类有几个字段,现在对于每个字段,我有一个如上所示的方法,我想知道是否有可能创建一个特殊变量,允许我访问产品中的每个字段,而不需要实际键入字段名或getter,而不是我只需键入的p.price/p.productNamep、 变量,根据该变量,我将访问所需的字段/getter
如果有这样的选择,如果你能告诉我在网上搜索什么,我将不胜感激
p、
非常感谢所有的快速回复,我正在检查它们。您可以根据输入变量组合查询,如下所示:
var inStock =
from p in products
select p;
if(inStock1 == InStock.needToOrder)
{
inStock = (from p in inStock where p.INStock == InStock.needToOrder || instock1 == InStock.False select p);
}
else
{
inStock = (from p in inStock where p.INStock == inStock1 select p);
}
return inStock.ToList<Product>();
你是说使用枚举之类的东西吗
internal enum InStock
{
NeedToOrder,
NotInStock,
InStock
}
internal class Product
{
public string Name { get; set; }
public InStock Stock { get; set; }
}
然后将集合传递给方法
internal static List<Product> ProductInStockQuery(List<Product> products)
{
var inStock =
from p in products
where p.Stock != InStock.NeedToOrder && p.Stock != InStock.NotInStock
select p;
return inStock.ToList();
}
创建一个列表并传入
var prods = new List<Product>
{
new Product
{
Name = "Im in stock",
Stock = InStock.InStock
},
new Product
{
Name = "Im in stock too",
Stock = InStock.InStock
},
new Product
{
Name = "Im not in stock",
Stock = InStock.NotInStock
},
new Product
{
Name = "need to order me",
Stock = InStock.NotInStock
},
};
var products = ProductInStockQuery(prods);
我相信您可以使用WhereIf-LINQ扩展方法使其更加清晰和可重用。下面是一个扩展示例
public static class MyLinqExtensions
{
public static IQueryable<T> WhereIf<T>(this IQueryable<T> source, Boolean condition, System.Linq.Expressions.Expression<Func<T, Boolean>> predicate)
{
return condition ? source.Where(predicate) : source;
}
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, Boolean condition, Func<T, Boolean> predicate)
{
return condition ? source.Where(predicate) : source;
}
}
下面您可以看到如何使用此方法的示例
class Program
{
static void Main(string[] args)
{
var array = new List<Int32>() { 1, 2, 3, 4 };
var condition = false; // change it to see different outputs
array
.WhereIf<Int32>(condition, x => x % 2 == 0) // predicate will be applied only if condition TRUE
.WhereIf<Int32>(!condition, x => x % 3 == 0) // predicate will be applied only if condition FALSE
.ToList() // don't call ToList() multiple times in the real world scenario
.ForEach(x => Console.WriteLine(x));
}
}
如何使和条件都为真,它将一次保存一个值,顺便问一下InStock的定义是什么?您可以使用“let”运算符在查询中创建变量,但不确定您实际要求的是什么。问题并不十分清楚。问题是如何能够返回不只是产品列表,还可能是其他内容列表的内容?InStock是一个枚举,包含:false、true、,needToOrder我现在会搜索'let'运算符,感谢您更好地解释我自己:我的类有几个字段,现在对于每个字段,我有一个如上所示的方法,我想知道是否可以创建一个特殊变量,允许我访问产品中的每个字段,而不必实际键入字段名或getter与p.price/p.productName类似,我只需键入p.VARIABLE,并根据此变量访问所需字段/getter@GalSheldon请将这些解释添加到您的原始帖子中。这是最简单的方法,但需要随时添加多个条件,这会使您的代码变得丑陋。我相信作者正在为这个问题寻找一个普遍的解决方案。你的第一句话是多余的。您正在从产品中选择所有p,而不改变任何内容是的-这只是一个示例。虽然-您需要这样做,但如果您想根据其他标准进行合成,etcq将抛出一个错误,您需要:-q=产品选择p中的p;顺便说一句,我不是落选者,这也是完全多余的
class Program
{
static void Main(string[] args)
{
var array = new List<Int32>() { 1, 2, 3, 4 };
var condition = false; // change it to see different outputs
array
.WhereIf<Int32>(condition, x => x % 2 == 0) // predicate will be applied only if condition TRUE
.WhereIf<Int32>(!condition, x => x % 3 == 0) // predicate will be applied only if condition FALSE
.ToList() // don't call ToList() multiple times in the real world scenario
.ForEach(x => Console.WriteLine(x));
}
}