3

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

http://msdn.microsoft.com/en-us/library/bb397947.aspx

http://msdn.microsoft.com/en-us/library/bb397678.aspx

1
  • As is normal with var - if you didn't use it, you wouldn't have the problem.
    – Amy B
    Mar 3, 2011 at 15:10

7 Answers 7

7

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 Single, SingleOrDefault, First, 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)

2
  • You're welcome. The most important thing here is to remember that, as Aasmund said in his answer, var is not a type, but instead gets replaced at compile-time by the type inferred from the expression on the right — when using the Select method from EF this will be an IQueryable<T> (IEnumerable<T> with Linq to objects); when using Single or the other methods, you get T instead.
    – madd0
    Mar 3, 2011 at 14:29
  • Once again thanks for your comment. It helps me to grasp this concept.bye
    – GibboK
    Mar 4, 2011 at 6:29
5

The LINQ 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();
5

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; 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 querySlotOrder.Single().

0
3

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;
2

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.

0

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

-3

Why do you use a var? if you know the type of the object you expect you should type querySlotOrder:

MyObjectType querySlotOrder = (from slot in context.CmsSlots
                             where slot.SlotId == myCurrentSlotId
                             select slot).FirstOrDefault();

           if (querySlotOrder.SlotOrder == myNewSlotOrder)
                e.Cancel = true;
2
  • 3
    You aren't answering his question, but rather are questioning his style. Lots of people use var, including many of the Linq samples. Nothing wrong with it. Regardless, your code is incorrect anyways. Mar 3, 2011 at 14:14
  • This won't compile. Assuming each slot item is of type MyObjectType, the result query will be of type IQueriable<MyObjectType>, not of MyObjectType. Mar 3, 2011 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.