LINQ:过滤多个字符串值 - 部分匹配 - 不像多个在线示例中那样完全匹配

时间:2015-08-14 10:11:28

标签: arrays string linq filter

在LINQ e.g.

中搜索多个字符串值有很多很好的例子
public static Product[] GetProducts(Guid[] prodIDs)
{
   return (from p in GetProducts()
           where prodIDs.Contains(p.ProductID)
           select p).ToArray<Product>();
}

我有一份我需要与客户匹配的产品清单, 但我没有完全匹配 - 客户产品列表包含我的产品ID - 但它并不准确 - 例如。

Customer             MyCompany
Description          Description
Prod1XY              Prod1
AProd2B              Prod2
XXXProd3             Prod3

因此我无法从prodID [string array]中过滤,因为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());
   }
}

2 个答案:

答案 0 :(得分:2)

您要完成的目标尚不清楚,但假设您要选择ProductID包含指定列表中的任何值的所有产品,它看起来就像它:

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

答案 1 :(得分:2)

试试这个

Sub DataTransfer()

Const FPATH As String = "C:\Users\bsmallwood\Desktop\FRF_Location_Data\"

Application.ScreenUpdating = False

Dim wb As Workbook
Dim shtAlpha As Worksheet 'Template
Dim locs, loc
Dim rngDest As Range

locs = Array("location1.xlsm", "location2.xlsm", _
             "location3.xlsm", "location4.xlsm")

Set shtAlpha = Workbooks("FRF_Data_Sheet_Template.xlsm").Sheets("DataInput")

'set the first data block destination
Set rngDest = shtAlpha.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Resize(5, 3)

For Each loc In locs

    Set wb = Workbooks.Open(FileName:=FPATH & loc, ReadOnly:=True)

    rngDest.Value = wb.Sheets("Data").Range("I3:K7").Value

    wb.Close False

    Set rngDest = rngDest.Offset(0, 3) 'move over to the right 3 cols

Next loc

Application.ScreenUpdating = True

End Sub