Linq VAR and Typed Object

血红的双手。 提交于 2020-01-14 14:19:29

问题


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:


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:


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();



回答3:


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().




回答4:


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;



回答5:


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.




回答6:


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




回答7:


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;


来源:https://stackoverflow.com/questions/5181753/linq-var-and-typed-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!