2015-08-14 46 views
0

e.g.LINQ:過濾多個字符串值 - 部分匹配 - 不完全匹配在多個網上的例子

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

我的產品列表,我需要在LINQ搜索多個字符串值的許多很好的例子以匹配客戶, 但我沒有完全匹配 - 客戶產品列表包含我的產品ID - 但它不是確切的 - 例如

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()); 
    } 
} 

回答

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>(); 
} 
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>(); 
} 
+0

輝煌,太感謝你了 –