开发者

problem in linq with comma separation of string

开发者 https://www.devze.com 2022-12-13 07:37 出处:网络
I am using the following to convert comma separated string to list. string productId =\"1开发者_运维百科,2\";

I am using the following to convert comma separated string to list.

string productId ="1开发者_运维百科,2";
string productName = "Product1,Product2";
string prodCat = "Cat1,Cat1";
string quantity = "10,10";
string retailPrice = "12,12";

var _productId = new List<String>(productId.Trim().Split(','));
var _productName = new List<String>(productName.Trim().Split(','));
var _prodCat = new List<String>(prodCat.Trim().Split(','));
var _quantity = new List<String>(quantity.Trim().Split(','));
var _retailPrice = new List<String>(retailPrice.Trim().Split(','));
var _products = (from pi in _productId
                join pro in _productName on _productId.IndexOf(pi) equals _productName.IndexOf(pro)
                join pn in _prodCat on _productId.IndexOf(pi) equals _prodCat.IndexOf(pn)
                join rp in _retailPrice on _productId.IndexOf(pi) equals _retailPrice.IndexOf(rp)
                join q in _quantity on _productId.IndexOf(pi) equals _quantity.IndexOf(q)
                where pi.Length > 0 && pro.Length > 0 
                select pi);

_products.Dump("Products");

The Above query returns the different result:

Products

IEnumerable<String> (8 items)  
1
1
1
1
1
1
1
1

But it should be:

Products

IEnumerable<String> (2 items)  
1
2

If i have different values in all the strings, i get the actual result. Here in the above example, i have the same category, quantity and price for two different products. But i am getting the result with eight wrong values.

Any clue on this, why it happens so?


Are you trying to do something like this?

var _products = Enumerable.Range(0, _productId.Count)
                          .Select(i => new
                              {
                                  Id = _productId[i],
                                  Name = _productName[i],
                                  Cat = _prodCat[i],
                                  Quantity = _quantity[i],
                                  RetailPrice = _retailPrice[i]
                              });


It looks like you are reading CSV file. If that is the case you better use CsvReader or other library. There are just small things in the CSV file that can be missed out.

As for your question. Why not just build normal objects and use normal Linq? Like this:

class Product {
    public string Id { get; set; }
    public string Name { get; set; }
    public string Category {get; set; }
    public int Quantity { get; set; }
    public decimal RetailPrice { get; set; }
}

IList<Product> products = new List<Product>();
for (int i=0; i < _productId.Length; i++) {
    products[i] = new Product {
        Id = _productId[i],
        Name = i < _productName.Length ? _productName[i] : null,
        Category = i < _prodCat.Length ? _prodCat[i] : null,
        Quantity= i < _quantity.Length ? int.Parse(_quantity[i]) : 0 // etc
    };
}

// Then use normal Linq2Objects:
_products = products
    .Where(c => !string.IsNullOrEmpty(c.Category))
    .Where(n => !string.IsNullOrEmpty(n.Name))
    .Select(x => x.Id);
_products.Dump("Products")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号