【问题标题】:LINQ : filter multiple string values - partial match - not full match as in multiple online examplesLINQ:过滤多个字符串值 - 部分匹配 - 不像多个在线示例中的完全匹配
【发布时间】:2015-08-14 10:11:28
【问题描述】:

在LINQe.g.中有很多搜索多个字符串值的好例子

public static Product[] GetProducts(Guid[] prodIDs)
{
   return (from p in GetProducts()
           where prodIDs.Contains(p.ProductID)
           select p).ToArray<Product>();
}

我有一个需要从客户那里匹配的产品列表, 但我没有完全匹配 - 客户产品列表包含我的 ProductID - 但它不准确 - 例如

Customer             MyCompany
Description          Description
Prod1XY              Prod1
AProd2B              Prod2
XXXProd3             Prod3

因此我无法从 prodIDs [字符串数组] 中过滤,因为 Prod1 不包含 Prod1XY 因此无法使用可用的示例。

我怎样才能有效地改变(反转)工作示例 请搜索包含我的产品描述的 CustomerProducts?

所以要确认:这不是重复的。示例使用string[] x 输入参数然后搜索: 其中x.contains

我需要帮助才能得到它:myProducts.Contains(x)

另一个在线示例修改以显示情况:

static void Main(string[] args) {

   var table = new[] {
      new { uid = 1 },
      new { uid = 2 },
      new { uid = 3 },
      new { uid = 4 },
      new { uid = 5 }
   };

   var stringarray = new[] { "1", "5", "10" };

   var results = from xx in table
                 where table.Contains(stringarray)
                 select xx;

   foreach (var result in results) {
      Console.WriteLine("Result: " + result.uid.ToString());
   }
}

【问题讨论】:

    标签: arrays string linq filter


    【解决方案1】:

    您要完成的工作还不够清楚,但假设您要选择 ProductID 包含指定列表中任何值的所有产品,它看起来像这样:

    public static Product[] GetProducts(string[] prodIDs)
    {
       return (from p in GetProducts()
               where prodIDs.Any(id=>p.ProductID.Contains(id))
               select p).ToArray<Product>();
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

      public static Product[] GetProducts(string[] prodIDs)
      {
         return (
              from p in GetProducts()
              from q in prodIDs
              where p.ProductID.IndexOf(q) > -1
              select p)
              .ToArray<Product>();
      }
      

      【讨论】:

        猜你喜欢
        • 2019-10-04
        • 2022-12-17
        • 2017-11-03
        • 2016-01-08
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-09
        相关资源
        最近更新 更多