I would like a sample of code.
At the moment I use linq in c# and asp.net 4 ef4
var querySlotOrder = from slot in context.CmsSlots
where slot.SlotId == myCurrentSlotId
select slot;
if (querySlotOrder.SlotOrder == myNewSlotOrder)
e.Cancel = true;
This linq query return only a record.
Using VAR I cannot get the Typed Object and I cannot access its property SlotOrder.
How to change the query? thanks for your help
Usefully resource on the topic:
http://msdn.microsoft.com/en-us/library/bb384065.aspx
Even if your query returns a single object, the
Selectmethod, which you are using behind the scenes, doesn’t. It returns anIQueryable<T>in EF.You should use a method such as
Single,SingleOrDefault,First,FirstOrDefaultif you want to store a single object.The difference between the four methods is:
Single: Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.SingleOrDefault: Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.First: Returns the first element of a sequence.FirstOrDefault: Returns the first element of a sequence, or a default value if the sequence contains no elements.(Definitions from MSDN)