如何仅返回条件上的特定对象列表基础

发布于 2025-02-06 21:24:59 字数 1811 浏览 3 评论 0原文

我想为返回选定的对象列表添加条件。如果情况满足条件,则如果该项目的类别是此示例,则不会返回该项目,类别为3。

这是查询:

var objectQuery = query
    .ToList()
    .Select(a => new
    {
        a.Id,
        ObjectDetails = (new ObjectClass[] { })
            .Concat(...)
            .Concat(...)
            .Distinct()
            .Select(f => new
            {
                f.ObjectDetails.Id,
                f.ObjectDetails.Name,
                f.ObjectDetails.DisplayName,
                category = (int)f.ObjectDetails.Category,
                f.ObjectDetails.IsArchived,
            })
    });

return objectQuery.ToList().FirstOrDefault();

我想添加这样的条件使用类别3并返回所有项目(如果没有),但是我不知道如何正确实施它:

if(condition a)
    return objectQuery.Select(...Where(category != 3)).ToList().FirstOrDefault();
else
    return objectQuery.ToList().FirstOrDefault();

这是回报输出:

{
  "objectQuery": {
    "id": 1198,
    "ObjectDetails": [
      {
        "id": 1,
        "name": "name one",
        "displayName": "NAME 1",
        "Category": 1,
      },
      {
        "id": 2,
        "name": "name two",
        "displayName": "NAME 2",
        "Category": 1,
      },
      {
        "id": 3,
        "name": "name three",
        "displayName": "NAME 3",
        "Category": 2,
      },
      {
        "id": 4,
        "name": "name four",
        "displayName": "NAME 4",
        "Category": 2,
      },
      {
        "id": 5,
        "name": "name five",
        "displayName": "NAME 5",
        "Category": 3,
      },
      {
        "id": 6,
        "name": "name six",
        "displayName": "NAME 6",
        "Category": 1,
      },
      {
        "id": 7,
        "name": "name seven",
        "displayName": "NAME 7",
        "Category": 1,
      }
    ]
  }
}

I want to add a condition to for returning selected object list. If the condition is met, it will not return the an item if its category is for this example, category is 3.

This is the query:

var objectQuery = query
    .ToList()
    .Select(a => new
    {
        a.Id,
        ObjectDetails = (new ObjectClass[] { })
            .Concat(...)
            .Concat(...)
            .Distinct()
            .Select(f => new
            {
                f.ObjectDetails.Id,
                f.ObjectDetails.Name,
                f.ObjectDetails.DisplayName,
                category = (int)f.ObjectDetails.Category,
                f.ObjectDetails.IsArchived,
            })
    });

return objectQuery.ToList().FirstOrDefault();

i want to add a condition like this where if it met the condition, it will remove all items with category 3 and returns all the item if not, but i dont know how to properly implement it:

if(condition a)
    return objectQuery.Select(...Where(category != 3)).ToList().FirstOrDefault();
else
    return objectQuery.ToList().FirstOrDefault();

this is the return output:

{
  "objectQuery": {
    "id": 1198,
    "ObjectDetails": [
      {
        "id": 1,
        "name": "name one",
        "displayName": "NAME 1",
        "Category": 1,
      },
      {
        "id": 2,
        "name": "name two",
        "displayName": "NAME 2",
        "Category": 1,
      },
      {
        "id": 3,
        "name": "name three",
        "displayName": "NAME 3",
        "Category": 2,
      },
      {
        "id": 4,
        "name": "name four",
        "displayName": "NAME 4",
        "Category": 2,
      },
      {
        "id": 5,
        "name": "name five",
        "displayName": "NAME 5",
        "Category": 3,
      },
      {
        "id": 6,
        "name": "name six",
        "displayName": "NAME 6",
        "Category": 1,
      },
      {
        "id": 7,
        "name": "name seven",
        "displayName": "NAME 7",
        "Category": 1,
      }
    ]
  }
}

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

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

发布评论

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

评论(1

时光无声 2025-02-13 21:24:59

如果我正确地得到您的问题,那么您将需要类似的问题:

var objectQuery = query
    .ToList()
    .Select(a => new
    {
        a.Id,
        ObjectDetails = (new ObjectClass[] { })
            .Concat(...)
            .Concat(...)
            .Distinct()
            .Select(f => new
            {
                f.ObjectDetails.Id,
                f.ObjectDetails.Name,
                f.ObjectDetails.DisplayName,
                category = (int)f.ObjectDetails.Category,
                f.ObjectDetails.IsArchived,
            })
            .Where(f => !condition || f.category != 3)
    });

return objectQuery.ToList().FirstOrDefault();

If I get your question properly then you'll need something like this:

var objectQuery = query
    .ToList()
    .Select(a => new
    {
        a.Id,
        ObjectDetails = (new ObjectClass[] { })
            .Concat(...)
            .Concat(...)
            .Distinct()
            .Select(f => new
            {
                f.ObjectDetails.Id,
                f.ObjectDetails.Name,
                f.ObjectDetails.DisplayName,
                category = (int)f.ObjectDetails.Category,
                f.ObjectDetails.IsArchived,
            })
            .Where(f => !condition || f.category != 3)
    });

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