0

I am trying to grab a single column from my linq query and put it into another list may not be the right way of doing it so please excuse me if my code is not good. Wanting to learn more about linq.

List<StockM> _stockm = new List<StockM>();
List<priceLists> _priceList = new List<priceLists>();
stockm = _db.getStockBySupplierCode(_supplierCode);

foreach (var stockitem in _stockm)
{
    _priceList = _db.getPriceTypesByProductCode(stockitem.product, "LPS");
    stockitem.lpePrice = Convert.ToDecimal(_priceList.Select(s => s.lpsPrice));
}

I think the probelems lies in how I am attempting to select the column out here

_priceList.Select(s => s.lpsPrice)

Bender Bending
  • 729
  • 8
  • 25
  • `_priceList` is a collection of object. You cannot convert a collection of object to a decimal. What do you want to extract from this list to store it into `lpePrice`? – fharreau Jul 19 '17 at 16:11

2 Answers2

0

try using FirstorDefault() as your _priceList is a list and you are assigning it to a value

_priceList.Select(s => s.lpsPrice).FirstOrDefault();

Also, do you need a where to find a particular value that matches? Just curious

FortyTwo
  • 2,414
  • 3
  • 22
  • 33
CuriousRK
  • 69
  • 7
0

There is several things wrong in your code.

First of all, I encourage you to read the .NET convention. There is no leading _ in local variable, the properties must start with an upper case, for examples.

Then, you do not need to instantiate a default instance if you are going to assign another instance to your variable next. You can assign it directly:

List<StockM> _stockm = _db.getStockBySupplierCode(_supplierCode);

Finally, the Select extension method you are using is already doing a foreach:

var result = _priceList.Select(s => s.lpsPrice);

Is basically (not exactly but it is a start to explain it like this) the same as:

var result = new List<decimal>();
foreach(var priceList in _priceList)
{
    result.Add(priceList.lpsPrice);
}

That being said, you can improve your code like this:

List<StockM> stockm = _db.getStockBySupplierCode(_supplierCode);

foreach (var stockitem in stockm)
{
    List<priceLists> _priceList = _db.getPriceTypesByProductCode(stockitem.product, "LPS");

    stockitem.lpePrice = ??? // you cannot use the Select here as it returns a collection of item. What do you really want to achieve?
 }
fharreau
  • 2,105
  • 1
  • 23
  • 46
  • I want to return a column's value out of the _priceList collection and suppose to naming convention you can have a underscore in a property of a method extension I use that to differentiate between in built properteys i might use to the ones and ones I create myself. Standard practise in .net Please see the second posting in this stack https://stackoverflow.com/questions/450238/to-underscore-or-to-not-to-underscore-that-is-the-question –  Jul 19 '17 at 16:10
  • Yes, you can use `_camelCase` for class fields. But here you are defining local methods variable. That's not the same thing. – fharreau Jul 19 '17 at 16:25
  • not necessarily just for class fields for properties as well its quite common practise actually –  Jul 19 '17 at 16:27
  • This is my guess, tell me if I am wrong: `priceList ` is a collection object returned from your database. You want to extract one column of these objects (`lpsPrice` which is I guess a decimal) and store it into `stockitem.lpePrice`. Guessing `lpePrice` is a decimal (because of the Convert.ToDecimal), you cannot store a list of decimal (take a look to my explanation of how `Select` works) into a decimal. So what do you want to do with your list of priceList? – fharreau Jul 19 '17 at 16:32
  • Here are my guess of what you could want to do: 1) You can sum all the `lpsPrice` of the collection to store it into `stockitem.lpePrice`. 2) You can take one specific priceList from the collection and store this specific `priceList.lpsPrice` into `stockitem.lpePrice`. Etc... – fharreau Jul 19 '17 at 16:38
  • its not a sum its a copy value so it is –  Jul 20 '17 at 07:20
  • What's the type of `stockitem.lpePrice` and the type of `priceList.lpsPrice`? – fharreau Jul 20 '17 at 07:37