bmanmike39
asked on
How do I add WHERE Clause to my entity query, to return a specific record?
I am trying to select a specific record from the db using entity, but can't figure out how.
This is what is going on. I'm trying to pass the record item_NumberID in to the query from a label control named lbQueryValue then put the record values into variables.
One other thing: because the table nlaCatalog does not have a relationship to any other table, it has no Navigation Properties.
Thanks
This is what is going on. I'm trying to pass the record item_NumberID in to the query from a label control named lbQueryValue then put the record values into variables.
// This is the select query
nlaCatalog ctlog = new nlaCatalog();
// this line doesn't work
nlaCatalog ctlog = ctlog.ToList().Where(x => x.Item_NumberID == int.Parse(lbQueryValue.Text.ToString())).First();
string itImage = ctlog.Item_Image.ToString();
string itName = ctlog.Item_Name.ToString();
string itDiscription = ctlog.Description.ToString();
string itPrice = ctlog.Price.ToString();
One other thing: because the table nlaCatalog does not have a relationship to any other table, it has no Navigation Properties.
Thanks
ASKER
I get an red squiggly line under ctlog.ToList()
... the error message is: dose not contain a definition for 'ToList' and no extension method
... the error message is: dose not contain a definition for 'ToList' and no extension method
Do you have a using System.Linq at the top of your file? Can you provide the code for nlaCatalog? If not, does nlaCatalog implement IEnumerable and IEnumerable<T>?
-saige-
-saige-
ASKER
Yes i have using Linq. but as i said, in my .edmx it show no Navigation Properties. I have Nav Properties on some of the other tables with relationships if this matters
Navigation properties should not be an issue here:
It all depends on your implementation of nlaCatalog. Does it carry a list of rows inside? Does it implement IEnumerable<T> and IEnumerable. What type does it inherit from? That is what is really at issue here.
In order for you to use a linq statement, the class in question has to be either Enumerable/Queryable or the class contains a/many member(s) that is/are Enumerable/Queryable.
-saige-
Navigation properties in the Entity Framework provide a way to navigate an association between two entity types.
It all depends on your implementation of nlaCatalog. Does it carry a list of rows inside? Does it implement IEnumerable<T> and IEnumerable. What type does it inherit from? That is what is really at issue here.
In order for you to use a linq statement, the class in question has to be either Enumerable/Queryable or the class contains a/many member(s) that is/are Enumerable/Queryable.
-saige-
What is the variable that contains a instance of the DbContext?
Does the variable nlaCatalog map to the table in the database?
Does the variable nlaCatalog map to the table in the database?
ASKER
When I'm typing it show all the rows in intellisense. regarding the IEnumerable<T> I cant remember how to view and check that?
In the class definition for nlaCatalog, you would see something like:
-saige-
public class nlaCatalog : IEnumerable<DataRow>
-saige-
Hi bmanmike39;
You are working with Entity Framework and NOT DataSet, DataTable objects correct?
You are working with Entity Framework and NOT DataSet, DataTable objects correct?
I believe he is Fernando. I'm just waiting to see what his class definition contains. From his code example, I can only assume he believes that nlaCatalog is a datatable.
-saige-
-saige-
ASKER
I see that i have errored in my context it's NLAMEMEntities2 The variable is int ctNumber
Im still getting the same error.
NLAMEMEntities2 ctlog = new NLAMEMEntities2();
int ctNumber = ctlog.ToList().Where(x => x.Item_NumberID.Equals(id)).FirstOrDefault();
Im still getting the same error.
ASKER
and searched for IEnumerable but can't find it in the app_code files
That is because unless NLAMEMEntities2 inherits from int, you are trying to cast the return from your linq statement, which would be an Enumerable<NLAMEMEntities2 > into an integer value.
-saige-
-saige-
What is the class definition for nlaCatalog? If you provide that, we can probably provide better assistance rather than stabbing in the dark.
-saige-
-saige-
Hi bmanmike39;
Try it as follows.
Try it as follows.
// Assuming that the DbContext is as follows
NLAMEMEntities2 ctlog = new NLAMEMEntities2();
// Assuming that nlaCatalog is the table in the database you wish to query, then try this.
nlaCatalog record = null;
int id = -1;
if (int.TryParse(lbQueryValue.Text, out id))
{
// Note that nlaCatalogs may need to be adjusted depending on how pluralization is configured on your system
record = ctlog.nlaCatalogs.Where(x => x.Item_NumberID == id).FirstOrDefault();
}
If the record variable is null the record was not found and if it has a non null value it is the record you are looking for, so test to make sure it has a value before using it.
ASKER
nlaCatalog is a table in the db, that i added through update model.
strangly the following code returns the data in a gridview. sorry, I just a bit confused now.
strangly the following code returns the data in a gridview. sorry, I just a bit confused now.
NLAMEMEntities2 ctlog = new NLAMEMEntities2();
GridView1.DataSource = ctlog.nlaCatalogs.ToList();
GridView1.DataBind();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, Excellent!
@bman, while I appreciate the points, Fernando came up with your solution. I feel he should get the bulk, if not all, of the points for this one.
Should accept as answer: https://www.experts-exchange.com/questions/28549962/How-do-I-add-WHERE-Clause-to-my-entity-query-to-return-a-specific-record.html?anchorAnswerId=40420798#a40420798
-saige-
Should accept as answer: https://www.experts-exchange.com/questions/28549962/How-do-I-add-WHERE-Clause-to-my-entity-query-to-return-a-specific-record.html?anchorAnswerId=40420798#a40420798
-saige-
Thanks -saige- for your recognition on this question but I believe that it would be fair to award the point as 50 / 50 seeming we both contributed to solving this question.
Here is one thing I would do:
Open in new window
-saige-