Click here to Skip to main content
16,018,458 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I need list value to variables as decimal unil=lst.Unit and so on..
Please help me out ..

Quote:

var lst = BusinessLayer.restdetailsBAL.Instance.GetRestDetails(Convert.ToInt32(lblID.Text)).Select(q=> new {
Unit=q.Unit,
SelPrice=q.SellPrice,
Batch=q.Batch,
Expiry=q.Expiry
}).ToList();


What I have tried:

how every i try :-
string s = lst.Select(q => q.Unit);
but there is(Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<decimal>' to 'string');
Posted
Updated 9-May-16 3:01am
Comments
BillWoodruff 9-May-16 9:15am    
You need to map the data returned into an instance of 'q ... whatever that is. Show us the structure of 'q and maybe we can help.
Karthik_Mahalingam 9-May-16 9:35am    
Hi Bill
from the question and what i have tried , its clear that he needs to assign the value from the list to a variable..
[GetRestDetails(Convert.ToInt32(lblID.Text)] this piece of code denotes that, the method will return only one Item ( ID, guessed )
and he is trying to assign the value from the Select query to a string variable which resulted in cast exception.

1 solution

try this

C#
var item = BusinessLayer.restdetailsBAL.Instance.GetRestDetails(Convert.ToInt32(lblID.Text)).Select(q => new
         {
             Unit = q.Unit,
             SelPrice = q.SellPrice,
             Batch = q.Batch,
             Expiry = q.Expiry
         }).FirstOrDefault();

         if (item != null)
         {
             string unit = item.Unit;
             var SelPrice = item.SelPrice;
             var Batch = item.Batch;
             var Expiry = item.Expiry;
         }


refer these
FirstOrDefault[^]
SingleOrDefault[^]
Difference Between First() and FirstOrDefault() in LINQ[^]
 
Share this answer
 
v4
Comments
Philippe Mori 9-May-16 12:34pm    
Well, you should also rename variables appropriatly since lst is not a list anymore.
Karthik_Mahalingam 9-May-16 12:37pm    
done

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900