2016-02-04 87 views
-1

我在使用LINQ查询对象数组时遇到了一些麻烦。我想检索包含传递值的所有产品。Linq Object Array

我的产品类

public class Product 
{ 
    public int mProductId; 
    public string mProductName; 
    public string mProductColor; 
    public string mProductSize; 
    public string mProductStatus; 
    public string mProductCode; 

    public int ProductId{ get { return mProductId; }} 
    public string ProductName { get{return mProductName; }} 
    public string ProductColor { get{return mProductColor;} } 
    public string ProductSize { get{return mProductSize;} } 
    public string ProductStatus { get{return mProductStatus;} } 
    public string ProductCode {get { return mProductCode; }} 
} 

public class ProductList 
{ 
    public static Product[] mProductList = { 
     new Product { mProductId = Resource.Drawable.Product1, 
      mProductName = "Green Lumberjack Cap", 
      mProductColor = "Color Brown", 
      mProductSize = "One Size Fits All", 
      mProductCode= "9780201760439", 
      mProductStatus= "In Stock"}, 
     new Product { mProductId = Resource.Drawable.Product2, 
      mProductName = "Square Bar stool", 
      mProductColor= "Color Brown", 
      mProductSize = "One Size Fits All", 
      mProductCode= "9780201760440", 
      mProductStatus= "In Stock"}, 
     new Product { mProductId = Resource.Drawable.Product3, 
      mProductName = "Vitra bathroom Tile", 
      mProductColor= "Color Brown", 
      mProductSize = "One Size Fits All", 
      mProductCode= "9780201760539", 
      mProductStatus= "In Stock"}, 


    }; 

    private Product[] mProducts; 
    Random mRandom; 

    public ProductList() 
    { 
     mProducts = mProductList; 

    } 

    // Return the number of photos in the photo album: 
    public int NumPhotos 
    { 
     get { return mProducts.Length; } 
    } 

    // Indexer (read only) for accessing a photo: 
    public Product this[int i] 
    { 
     get { return mProducts[i]; } 
    } 

    // Pick a random photo and swap it with the top: 
    public int RandomSwap() 
    { 
     // Save the photo at the top: 
     Product tmpProduct = mProducts[0]; 

     // Generate a next random index between 1 and 
     // Length (noninclusive): 
     int rnd = mRandom.Next(1, mProducts.Length); 

     // Exchange top photo with randomly-chosen photo: 
     mProducts[0] = mProducts[rnd]; 
     mProducts[rnd] = tmpProduct; 

     // Return the index of which photo was swapped with the top: 
     return rnd; 
    } 

    // Shuffle the order of the photos: 
    public void Shuffle() 
    { 
     // Use the Fisher-Yates shuffle algorithm: 
     for (int idx = 0; idx < mProducts.Length; ++idx) 
     { 
      // Save the photo at idx: 
      Product tmpProduct = mProducts[idx]; 

      // Generate a next random index between idx (inclusive) and 
      // Length (noninclusive): 
      int rnd = mRandom.Next(idx, mProducts.Length); 

      // Exchange photo at idx with randomly-chosen (later) photo: 
      mProducts[idx] = mProducts[rnd]; 
      mProducts[rnd] = tmpProduct; 
     } 
    } 



} 

和我的LINQ语句是

var result = from p in nProductList<Product> 
        where (p.mProductName.Contains(query) || p.mProductColor.Contains(query)) 
      select p; 

我也宣布nProductList在我班上的

public ProductList nProductList; 

这将是真正伟大知道我做错了吗?

谢谢

+0

如果你阅读[问],这也会很棒。具体阅读关于MCVE – Amit

+1

您需要公开'mProducts'字段以某种方式公开 - 最安全的是添加一个只读的IEnumerable '返回一个数组的枚举数(不是数组本身,因为恶意代码可以转换为数组并修改它)。 –

+0

@Amit:很高兴能按照我的要求做出任何改变。但是,请你告诉我我做错了什么。我已通读该文件,没有任何事情真的打动我 – LibinJoseph

回答

0

为了获得where关键字语法的工作,你的ProductList类必须在其上具有Where(Func<Product, bool>)方法。大多数列表会自动获取此信息,因为它们实现了IEnumerable<>,并且System.Linq名称空间具有匹配此签名的Where()扩展方法。

你可以做ProductList实现IEnumerable<Product>接口,或使其扩展一个类像List<Product>已实现该接口,或者添加自己的Where()方法。不过,我个人建议你通过一个公共属性获取器将mProductList作为IEnumerable<Product>公开,然后将你的使用代码改为查询。

0

你的linq语句不工作的原因是你没有定义在哪里。想象一下旧款式:

nProductList.Where(p=>p.mProductName.Contains(query) || p.mProductColor.Contains(query)).Select(p=>); 

nProductList没有定义Where(Func),所以它不起作用。

通常对于你的ProductList有两种实现方式。第一种方法是从IEnumerable<Product>继承为ProductList : IEnumerable<Product>; 第二种方式是在ProductList创建成员,并把它公开像

public class ProductList 
{ 
    public IEnumerable<Product> Products {get; private set;} 
    ... 
} 

通常情况下,两者之间的最好方式上面会取决于是否有更多的属性或在你的产品列表类的更多方法。更多的方法是第一种方式,因为它更像是一个IEnumerable类的扩展方法集合(比如你的例子),而更多的属性则采用借调的方式,因为这更像是另一个只有列表和其他类的类。

相关问题