【问题标题】:Select categories where no product has inventory选择没有产品库存的类别
【发布时间】:2012-01-06 15:50:29
【问题描述】:

我正试图围绕一个 Linq 查询,仅当所有子对象都包含特定属性时才返回父对象。

例如,返回类别,其中链接到该类别的所有产品都有product.Inventory == 0

此问题的其他标题:
选择所有 ChildObject 都具有特定 ChildObject.Parameter 值的 ParentObject

编辑:
除了关系之外,我还只想获取一个类别,如果它的日期属性之一不为空。

编辑:
这是我之前尝试过的示例之一:

var selectQuery = 
   (from statementDetail in pcardDatabaseContext.PCardStatementDetails
    where statementDetail.ExportedDate != null
    && statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved)
    orderby statementDetail.ExportedDate
    select statementDetail) as IOrderedQueryable<PCardStatementDetail>;

编辑:
为我的问题找到了解决方案,但 7 小时后无法自行回答。

我在早期的语法中部分遇到了一些问题,我假设在使用 x.All 时,如果集合为空,则值不会返回任何匹配项。

这是为我解决的问题:

var selectQuery =
   (from statementDetail in pcardDatabaseContext.PCardStatementDetails
    where statementDetail.ExportedDate == null
    && statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved)
    && statementDetail.PCardTransactions.Any()
    orderby statementDetail.ExportedDate
    select statementDetail) as IOrderedQueryable<PCardStatementDetail>;

请注意,我已将 ExportDate 修改为仅检索 ExportedDate == NULL。 另外,我必须添加一个.Any,否则我会得到没有事务的记录(我认为.All 会被过滤掉)。

【问题讨论】:

  • 这是我之前尝试过的一个例子。你能告诉我这是否应该工作吗? codevar selectQuery = (from statementDetail in pcardDatabaseContext.PCardStatementDetails where statementDetail.ExportedDate != null && statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved) orderby statementDetail.ExportedDate select statementDetail) as IOrderedQueryable; code

标签: c# linq entity-framework entity-relationship iqueryable


【解决方案1】:
var categoriesWithNoInventory =
    Categories.Where(c => c.Products.All(p => p.Inventory == 0));

【讨论】:

    【解决方案2】:

    假设你的类看起来像这样

    public class Category
    {
        public string Name { get; set; }
        public List<Product> Products { get; set; }
    }
    
    public class Product
    {
        public string Name { get; set; }
        public int Inventory { get; set; }
    }
    

    那么这将起作用(AllCategories() 返回IEnumerable&lt;Category&gt;

    var categories = AllCategories().Where(c => c.Products.All(p => p.Inventory == 0));
    

    【讨论】:

    • 嗨,马克,我正在尝试在枚举对象之前在单个 Queryable 中执行过滤。
    【解决方案3】:

    我认为您必须进行 2 次查询?首先抓取子产品然后检查库存,或者我想您可以通过连接来完成,这是一个连接示例:http://www.dotnetperls.com/join

    【讨论】:

    • 我看到了那个样本。我在正确处理它时遇到了一些麻烦。也尝试过加入和 groupJoins...
    • 我知道你还不能离开 cmets,但这并不是真正的答案。
    【解决方案4】:

    为我的问题找到了解决方案,但在另外 7 小时内无法自行回答。

    我在早期的语法中部分遇到了一些问题,我假设在使用 x.All 时,如果集合为空,则值不会返回任何匹配项。

    这是为我解决的问题:

    var selectQuery =
       (from statementDetail in pcardDatabaseContext.PCardStatementDetails
        where statementDetail.ExportedDate == null
        && statementDetail.PCardTransactions.All(txn => txn.TransactionStatusID == txnStatusAccountingApproved)
        && statementDetail.PCardTransactions.Any()
        orderby statementDetail.ExportedDate
        select statementDetail) as IOrderedQueryable<PCardStatementDetail>;
    

    请注意,我已将 ExportDate 修改为仅检索 ExportedDate == NULL。 另外,我必须添加一个.Any,否则我会得到没有事务的记录(我认为.All 会被过滤掉)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多