Solved

C# linq

Posted on 2013-01-26
4
309 Views
Last Modified: 2013-01-27
Below is the linq I am using to populate a datagridview. The PassFailFlag in the database is either 0 or 1 - representing fail or pass. I don't want the datagridview to display 0 and 1. I would like it to display the text "Fail" or "Pass".

public enum PFType { Fail, Pass };

dataGridViewSearch.DataSource = (from ph in DB.PanelHistories
                                                     join u in DB.Users on ph.UserID equals u.ID
                                                     where ph.UserID == (int)cboSearchOperator.SelectedValue
                                                     select new
                                                     {
                                                         ph.PartNumber,
                                                         ph.OrderNumber,
                                                         ph.PieceNumber,
                                                         ph.MeasurementDate,
                                                         (PFType) ph.PassFailFlag,
                                                         ph.MeasuredPOA,
                                                         ph.TestType,
                                                         u.UserName
                                                     });

The above doesn't work. I get the message "invalid anonymous type declarator".

What is the easiest way to get the Pass/Fail column in my datagridview to display the text "Fail" and "Pass" instead of 0 and 1?

Thanks!
0
Comment
Question by:neekolzun
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
Comment Utility
You can't map the LINQ directly to an Enum like that, but you can use a case statement. Replace the line that says:

(PFType) ph.PassFailFlag,

Open in new window


with the following:

PassFailFlag = 
        (
            ph.PassFailFlag == 0 ? "Fail" :
            ph.PassFailFlag == 1 ? "Pass" :
            "Other"
        )

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
or simply:

((PFType)ph.PassFailFlag).ToString()

Open in new window

0
 
LVL 28

Expert Comment

by:Ryan McCauley
Comment Utility
Good call, sedgwick - I always forget that you can just .ToString the boolean type to get its name.
0
 

Author Comment

by:neekolzun
Comment Utility
Just what I needed! Thank you so much!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now