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

I have this entity query and I want to capture the result so that I can use it in an if else statement or where ever.

C#
private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
IQueryable rs = db.Transmitals;
                    rs = db.Transmitals
                            .Select (sl => new Transmital { Serial = sl.Serial })
                            .Where  (sl => sl.Serial == 888)
                            .AsQueryable();


The way I look at it, it can execute the query but it returns an object. I want to put the result in a variable so I can use it in an if else statement or future purposes.

This is in a post method by the way. I am going to be comparing this result from a raw Json post from postman.

Edit:
Serials is a column in the Transmital table.
Posted
Updated 20-Apr-15 3:23am
v3
Comments
Sascha Lefèvre 20-Apr-15 9:16am    
What do you want to get as a result? A collection of "Transmital"-objects?
Joshua Masa 20-Apr-15 9:22am    
Yes, the Transmital table objects. Serial, by the way is a column in the Transmital table. I will add this in my question.

1 solution

This would get you a list of new (!) "Transmital"-objects, just like you created new objects in your posted code:
C#
private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
List<Transmital> rs = db.Transmitals
                        .Where  (sl => sl.Serial == 888)
                        .Select (sl => new Transmital { Serial = sl.Serial })
                        .ToList();


Of course you would have to know if that's what you need - to me it looks like it might be by mistake. A query that returns the "original" objects would be this:
C#
private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
List<Transmital> rs = db.Transmitals
                        .Where  (sl => sl.Serial == 888)
                        .ToList();


And then you could use rs in some if-statement. If all you would want to check in your if-statement was if there are any matching "Transmitals" then you could use a query that returns a bool instead:
C#
private AH_ODS_DB_Entities db = new AH_ODS_DB_Entities();
bool anyMatchingTransmitals = db.Transmitals
                                .Where  (sl => sl.Serial == 888)
                                .Any();


Likewise you could use .Count() instead of .Any() if you're interested in the amount of matching "Transmitals" (the return type would be int instead of bool then).

Does this help?
 
Share this answer
 
Comments
Joshua Masa 20-Apr-15 9:48am    
If I would like to return it as long? would I substitute bool to int64 perhaps?
Sascha Lefèvre 20-Apr-15 9:49am    
What should be the meaning of the returned value? The amount of matching records?
Joshua Masa 20-Apr-15 10:02am    
Basically I want to make a SELECT Serials FROM Transmital WHERE Serials = 888 using linq. Now I want what it returns to be put in a variable so I can use it in an if statement. It should return a long data since I will be comparing it to a long data from a JSON post.
Sascha Lefèvre 20-Apr-15 10:06am    
Then the middle part of my answer should be what you need. The size of the integer type doesn't matter, you can compare it to any other integer type (you can compare "int"-serials to "long"-serials).
Joshua Masa 20-Apr-15 11:05am    
Thank you. The count method works better for me.

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