【问题标题】:Can the EXCEPT operator exclude data where a column contains data from an arrayEXCEPT 运算符是否可以排除列包含数组中数据的数据
【发布时间】:2012-05-28 00:17:41
【问题描述】:

假设有一串以逗号分隔的文本值,如数组:

var excludelist ="apples,oranges,grapes,pears";

排除列表值可能来自对数据库中表的查询。

假设一个查询,我们想要返回所有行,除了那些名为 Fruit 的字段包含排除列表中的任何项目的行。

var qry = from s in context.Groceries.Where(s => s.Fruit(here is where we need to exclude the items??) join u in context.Users on s.Owner equals u.User_ID

有人可以提供指向 SQL 答案的示例链接吗?

【问题讨论】:

    标签: c# model-view-controller linq-to-sql


    【解决方案1】:

    你试过Except吗?

    var qry = from s in context.Groceries.Except(excludelist)...
    

    到 SQL 的链接有 CONTAINS(和!CONTAINS)

    where !excludelist.Contains(i)
            select i;
    

    【讨论】:

    • 排除列表仅存在于名为 Fruit 的 Groceries 字段/列中,因此我认为 Groceries.Except 不起作用。
    • 我也试过了并导致无法解释的 javascript 错误。
    【解决方案2】:

    我是这样解决的:

    我有一个包含我希望排除的水果名称的数据库表,我创建了一个列表(IList 显然不起作用)。

    List<string> excludedfruit = context.ExcludedFruit.Select(x => x.ExcludedFruitName).ToList();
    

    然后我使用了下面的 Linq to SQL 查询(部分显示)

            var qry = from s in context.Groceries
                    .Where(s => !excludedfruit.Contains(s.Fruit))
    

    【讨论】:

      猜你喜欢
      • 2015-08-29
      • 2022-09-30
      • 2013-10-17
      • 2016-12-14
      • 2020-02-22
      • 2023-03-26
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多