Even if your query returns a single object, the
Select method, which you are using behind the scenes, doesn't. It returns an
IQueryable<T> in EF.
You should use a method such as
FirstOrDefault if you want to store a single object.
var querySlotOrder = (from slot in context.CmsSlots where slot.SlotId == myCurrentSlotId select slot).Single();
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)
select-statement always returns a queryable collection. Therefore you need to fetch a single object from it.
var querySlotOrder = (from slot in context.CmsSlots where slot.SlotId == myCurrentSlotId select slot).FirstOrDefault();
The type of the returned object is
IQueryable<CmdSlot> (assuming that
CmdSlot is the type of the elements), and
querySlotOrder gets that type (that's the effect of
var itself is not a type). If you are absolutely sure that there will always be exactly one element in the result collection, you can retrieve it with
Why do you use a
var? if you know the type of the object you expect you should type
MyObjectType querySlotOrder = (from slot in context.CmsSlots where slot.SlotId == myCurrentSlotId select slot).FirstOrDefault(); if (querySlotOrder.SlotOrder == myNewSlotOrder) e.Cancel = true;
As Dennis is pointing out in his answer, you're not getting an instance of one object, you're getting an IEnumerable back from your query. In order to access the SlotOrder property you need to pick a particular item from the collection, most likely the first - judging by your query.
The linq query returns not a record, but a collection of records that has only 1 element. If you want to get the first element and if you are sure that there is only 1 element in the collection, use Single extension method:
var querySlotOrders = from slot in context.CmsSlots where slot.SlotId == myCurrentSlotId select slot; var querySlotOrder = querySlotOrders.Single(); if (querySlotOrder.SlotOrder == myNewSlotOrder) e.Cancel = true;
This is madd0's code but reformated to use C# style instead of SQL style
var querySlotOrder = context.CmsSlots .Where(slot => slot.SlotId == myCurrentSlotId) .Single();
it's better to read and understand whan SQL style
- LINQ Source Code Available
- Linq with where clause in many-to-many EF Code First object
- Linq VAR and Typed Object
- creating Linq to sqlite dbml from DbLinq source code
- Linq to sql as object data source - designer problem with partial classes
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- convert linq to object query to sql query (no linq to sql code or datacontext)
- source code for LINQ 101 samples
- Finding the maximum valued and different typed items in an object list by using Linq
- Linq code to get the index of an object in an array from an object within a list
- LINQ to XML - selecting XML to a strongly typed object
- Create a tree structure in linq with a single list source with parent - child as strings of an object
- Use LINQ to convert List of Dictionary to strongly Typed Class object List
- Check Nullable for object insde LinQ to Sql code
- c# Linq or code to extract groups from a single list of source data
- nullable object must have a value error in linq to sql in vb.net code
- How to use LINQ to select object with minimum or maximum property value
- Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)
- Convert string to int in one line of code using LINQ
- LINQ Group By into a Dictionary Object
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- How can I get LINQ to return the object which has the max value for a given property?
- Create a list from two object lists with linq
- Linq code to select one item
- How to get first object out from List<Object> using Linq
- Return list of specific property of object using linq
- How are people unit testing code that uses Linq to SQL
- LINQ selection by type of an object
- LINQ - Does the Where expression return new instance or reference to object instance
- LINQ to SQL: GroupBy() and Max() to get the object with latest date
- How to generate a full dynamic grid with Linq and JQuery DataTable?
- Problem after sorting a IEnumerable with LINQ in C#
- How to read data chunk wise into memory from the database?
- nested where query in Entity Framework
- loop ienumerable and get values
- Getting the sum of a value in Linq
- How to return FirstOrDefaultAsync EF Linq to entity List query
- Using LINQ Include after GroupBy and Take
- how do I write this query in linq ( c# )?
- Count each columns in linq with left join c#
- Convert List to List<string>
- List of object array from XML
- How to merge repeating node values into one node?
- Using LINQ to compare strings alphabetically
- Use variable from selected row on mysql
- Entity Framework Core: Include many-to-many related objects in WebAPI
- C# LINQ : Dynamically create property and values that includes sum of a value from a grouped query
- Getting an invalid cast exception when trying to order a list of objects with linq
- Query for returning properties from several entities
- Using Left Outer Join in Linq