Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// i m new for for Lambda expression and lurning it
C#
var getdata = dm.Cloths.Where(w => w.Cloth_No == txtvalue).Select(w => new { w.Cloth_Name, w.Cloth_Type });
           if (Enumerable.Count(getdata) > 0)
           {
               MessageBox.Show(????????);
// how can i print Cloth_Name and Cloth_Type in message box which  are in getdata  
           }
Posted
Updated 17-Nov-14 3:54am
v2
Comments
ZurdoDev 17-Nov-14 9:10am    
You can use the debugging tools, such as the Watch window to see what getdata is.

You want to formulate that a bit differently, and you have a typo. Assuming that you want to keep the list of anonymous objects around this method will work::

C#
var getdata = dm.Cloths.Where(w => w.Cloth_No == txtvalue).Select(w => new { w.Cloth_Name, w.Cloth_Type }).ToList();
           if (getdata.Any())
           {
               var sb = new StringBuilder();
               foreach(var item in getdata)
               {
                   sb.Append(item.Cloth_Name + " : " + item.Cloth_Type + "\n");
               }
               MessageBox.Show(sb.ToString()); 
           }
 
Share this answer
 
Comments
mukesh mourya 17-Nov-14 9:56am    
sorry is not "getdate", both are "getdata"
mukesh mourya 17-Nov-14 11:07am    
sir...

here in yr solu. you Contain item in StringBuilder and use foreach loop.

if i want to print this are item on diffrent -diffrent Label like Cloth_Name on lblCloth_Name (Label) and Cloth_Type (Label) on lblCloth_Type without make foreach loop ,then how can i do that.
Nathan Minier 17-Nov-14 11:23am    
Well, if you're expecting a single entry back from your query, that changes things.

Modify as follows:

var getdata = dm.Cloths.Where(w => w.Cloth_No == txtvalue).FirstOrDefault();
if (getdata.Any())
{
lblCloth_Name.Text = getdata.Cloth_Name;
lblCloth_Type.Text = getdata.Cloth_Type;
}

This will get exactly one (or none, if Cloth_No can't be found) results.
Nathan Minier 17-Nov-14 11:24am    
Well, if you're expecting a single entry back from your query, that changes things.

Modify as follows:

var getdata = dm.Cloths.Where(w => w.Cloth_No == txtvalue).Select(w => new { w.Cloth_Name, w.Cloth_Type }).FirstOrDefault();
if (getdata.Any())
{
lblCloth_Name.Text = getdata.Cloth_Name;
lblCloth_Type.Text = getdata.Cloth_Type;
}

This will get exactly one (or none, if Cloth_No can't be found) results.
mukesh mourya 17-Nov-14 11:34am    
sir....


here i got error in if (getdata.Any())
Bug: change this: Enumerable.Count(getdate) > To: Enumerable.Count(getdata) >

You may also want to execute the query projection you build using 'new in Linq to transform it into a usable List:

var getdata = dm.Cloths.Where(w => w.Cloth_No == txtvalue).Select(w => new { w.Cloth_Name, w.Cloth_Type }).ToList();

Put a break-point on the if statement, and, when your application stops there: inspect the contents of 'getdata: whatever is available inside 'getdata is available for you to show in a MessageBox, converting to String if necessary.

'dmCloths is a collection of a Class ?
 
Share this answer
 
Comments
mukesh mourya 17-Nov-14 9:55am    
sorry is not "getdate", both are getdata
BillWoodruff 17-Nov-14 10:24am    
That's why I suggested you change it :)

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