在这种情况下可以构建单个 LINQ 查询表达式吗?

发布于 2024-08-05 04:05:51 字数 1173 浏览 8 评论 0原文

我面临着一个场景,我必须根据许多对象过滤单个对象。

举例来说,我有一个 Grocery 对象,其中包含 Fruit 和 Vegetable 属性。然后我有单独的水果蔬菜对象。

我的目标是这样的:

var groceryList = from grocery in Grocery.ToList()
                  from fruit in Fruit.ToList()
                  from veggie in Vegetable.ToList()
                  where (grocery.fruitId = fruit.fruitId)
                  where (grocery.vegId = veggie.vegId)
                  select (grocery);

我面临的问题是水果和蔬菜对象为空时。 空,我的意思是它们的列表计数为 0,并且我只想在填充过滤器列表时应用过滤器。

我也不能能够使用类似的东西,因为对象为空:

var groceryList = from grocery in Grocery.ToList()
                  from fruit in Fruit.ToList()
                  from veggie in Vegetable.ToList()
                  where (grocery.fruitId = fruit.fruitId || fruit.fruitId == String.Empty)
                  where (grocery.vegId = veggie.vegId || veggie.vegId == String.Empty)
                  select (grocery);

所以,我打算检查水果蔬菜列表计数...并在连续过滤的 Grocery 对象上将它们作为单独的表达式进行过滤。

但是,如果单个查询表达式中存在空对象,是否有办法仍然获取列表?

I am facing a scenario where I have to filter a single object based on many objects.

For sake of example, I have a Grocery object which comprises of both Fruit and Vegetable properties. Then I have the individual Fruit and Vegetable objects.

My objective is this:

var groceryList = from grocery in Grocery.ToList()
                  from fruit in Fruit.ToList()
                  from veggie in Vegetable.ToList()
                  where (grocery.fruitId = fruit.fruitId)
                  where (grocery.vegId = veggie.vegId)
                  select (grocery);

The problem I am facing is when Fruit and Vegetable objects are empty.
By empty, I mean their list count is 0 and I want to apply the filter only if the filter list is populated.

I am also NOT able to use something like since objects are null:

var groceryList = from grocery in Grocery.ToList()
                  from fruit in Fruit.ToList()
                  from veggie in Vegetable.ToList()
                  where (grocery.fruitId = fruit.fruitId || fruit.fruitId == String.Empty)
                  where (grocery.vegId = veggie.vegId || veggie.vegId == String.Empty)
                  select (grocery);

So, I intend to check for Fruit and Vegetable list count...and filter them as separate expressions on successively filtered Grocery objects.

But is there a way to still get the list in case of null objects in a single query expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

踏月而来 2024-08-12 04:05:51

我认为 LINQ GroupJoin 运算符 会在这里为您提供帮助。它类似于 TSQL LEFT OUTER JOIN

I think the LINQ GroupJoin operator will help you here. It's similar to the TSQL LEFT OUTER JOIN

一向肩并 2024-08-12 04:05:51
IEnumerable<Grocery> query = Grocery

if (Fruit != null)
{
  query = query.Where(grocery =>
    Fruit.Any(fruit => fruit.FruitId == grocery.FruitId));
}

if (Vegetable != null)
{
  query = query.Where(grocery =>
    Vegetable.Any(veggie => veggie.VegetableId == grocery.VegetableId));
}

List<Grocery> results = query.ToList();
IEnumerable<Grocery> query = Grocery

if (Fruit != null)
{
  query = query.Where(grocery =>
    Fruit.Any(fruit => fruit.FruitId == grocery.FruitId));
}

if (Vegetable != null)
{
  query = query.Where(grocery =>
    Vegetable.Any(veggie => veggie.VegetableId == grocery.VegetableId));
}

List<Grocery> results = query.ToList();
小傻瓜 2024-08-12 04:05:51

尝试如下操作:

var joined = grocery.Join(fruit, g => g.fruitId,
                                 f => f.fruitId,
                                 (g, f) => new Grocery() { /*set grocery properties*/ }).
                Join(veggie, g => g.vegId,
                             v => v.vegId,
                             (g, v) => new Grocery() { /*set grocery properties*/ });

在我所说的设置杂货属性的地方,您可以从选择器的 g、f、v 变量设置杂货对象的属性。感兴趣的显然是设置 g.fruitId = f.fruitIdg.vegeId = v.vegeId

Try something like the following:

var joined = grocery.Join(fruit, g => g.fruitId,
                                 f => f.fruitId,
                                 (g, f) => new Grocery() { /*set grocery properties*/ }).
                Join(veggie, g => g.vegId,
                             v => v.vegId,
                             (g, v) => new Grocery() { /*set grocery properties*/ });

Where I have said set grocery properties you can set the properties of the grocery object from the g, f, v variables of the selector. Of interest will obviouly be setting g.fruitId = f.fruitId and g.vegeId = v.vegeId.

┈┾☆殇 2024-08-12 04:05:51
var groceryList =
  from grocery in Grocery.ToList()
  join fruit in Fruit.ToList()
       on grocery.fruidId equals fruit.fruitId
       into groceryFruits
  join veggie in Vegetable.ToList()
       on grocery.vegId equals veggie.vegId
       into groceryVeggies
  where ... // filter as needed
  select new
  {
    Grocery = grocery,
    GroceryFruits = groceryFruits,
    GroceryVeggies = groceryVeggies
  };
var groceryList =
  from grocery in Grocery.ToList()
  join fruit in Fruit.ToList()
       on grocery.fruidId equals fruit.fruitId
       into groceryFruits
  join veggie in Vegetable.ToList()
       on grocery.vegId equals veggie.vegId
       into groceryVeggies
  where ... // filter as needed
  select new
  {
    Grocery = grocery,
    GroceryFruits = groceryFruits,
    GroceryVeggies = groceryVeggies
  };
长亭外,古道边 2024-08-12 04:05:51

为此,您必须使用 leftouter join(如 TSQL)。在技​​巧的查询下方

private void test()
{
    var grocery = new List<groceryy>() { new groceryy { fruitId = 1, vegid = 1, name = "s" }, new groceryy { fruitId = 2, vegid = 2, name = "a" }, new groceryy { fruitId = 3, vegid = 3, name = "h" } };
    var fruit = new List<fruitt>() { new fruitt { fruitId = 1, fname = "s" }, new fruitt { fruitId = 2, fname = "a" } };
    var veggie = new List<veggiee>() { new veggiee { vegid = 1, vname = "s" }, new veggiee { vegid = 2, vname = "a" } };
    //var fruit= new List<fruitt>();
    //var veggie = new List<veggiee>();

    var result = from g in grocery
                 join f in fruit on g.fruitId equals f.fruitId into tempFruit
                 join v in veggie on g.vegid equals v.vegid into tempVegg
                 from joinedFruit in tempFruit.DefaultIfEmpty()
                 from joinedVegg in tempVegg.DefaultIfEmpty()
                 select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };

    foreach (var outt in result)
        Console.WriteLine(outt.fruitId + "  " + outt.vegid  + "  " + outt.fname  + "  " + outt.vname);
}
public class groceryy
{
    public int fruitId;
    public int vegid;
    public string name;
}
public class fruitt
{
    public int fruitId;
    public string fname;
}
public class veggiee
{
    public int vegid;
    public string vname;
}

编辑:
这是示例结果

1 1 ss

2 2 aa

3 3

You have to use leftouter join (like TSQL) for this. below the query for the trick

private void test()
{
    var grocery = new List<groceryy>() { new groceryy { fruitId = 1, vegid = 1, name = "s" }, new groceryy { fruitId = 2, vegid = 2, name = "a" }, new groceryy { fruitId = 3, vegid = 3, name = "h" } };
    var fruit = new List<fruitt>() { new fruitt { fruitId = 1, fname = "s" }, new fruitt { fruitId = 2, fname = "a" } };
    var veggie = new List<veggiee>() { new veggiee { vegid = 1, vname = "s" }, new veggiee { vegid = 2, vname = "a" } };
    //var fruit= new List<fruitt>();
    //var veggie = new List<veggiee>();

    var result = from g in grocery
                 join f in fruit on g.fruitId equals f.fruitId into tempFruit
                 join v in veggie on g.vegid equals v.vegid into tempVegg
                 from joinedFruit in tempFruit.DefaultIfEmpty()
                 from joinedVegg in tempVegg.DefaultIfEmpty()
                 select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };

    foreach (var outt in result)
        Console.WriteLine(outt.fruitId + "  " + outt.vegid  + "  " + outt.fname  + "  " + outt.vname);
}
public class groceryy
{
    public int fruitId;
    public int vegid;
    public string name;
}
public class fruitt
{
    public int fruitId;
    public string fname;
}
public class veggiee
{
    public int vegid;
    public string vname;
}

EDIT:
this is the sample result

1 1 s s

2 2 a a

3 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文