Solved

C# linq

Posted on 2013-01-26
4
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 38823578
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
ID: 38824003
or simply:

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

Open in new window

0
 
LVL 28

Expert Comment

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

Author Comment

by:neekolzun
ID: 38824749
Just what I needed! Thank you so much!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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