简体   繁体   English

如何转换IQueryable <Int> &#39;int&#39;

[英]How to convert IQueryable<Int> to 'int'

I am facing a problem when trying to get the last record of my model Client in terms of the userID. 尝试根据用户ID获取我的模型Client的最后一条记录时遇到问题。

var user_id = (int)Membership.GetUser().ProviderUserKey;
        var client_id = from d in db.Connexions 
                        where d.userId == user_id
                        select d.ClientID;

        var lastClient = from d in db.Clients
                         where d.ClientID = client_id // here appears the error
                         select d;

For the first one there is no error but for the second request there is an error of type 对于第一个请求,没有错误,但是对于第二个请求,存在错误类型

var client_id = (from d in db.Connexions 
            where d.userId == user_id
            select d.ClientID).First();

If you want to retrieve last record then do this : 如果要检索最后一条记录,请执行以下操作:

var lastClient = (from d in db.Clients
                  where d.ClientID == client_id  <--- //here it should be "==" not "="
                  orderby d.UpdateTime descending
                  select d).FirstOrDefault();

This code: 这段代码:

var client_id = from d in db.Connexions 
                    where d.userId == user_id
                    select d.ClientID;

does not return a single int, but a lazy collection of ints fulfilling the conditions given. 不返回单个int,而是满足给定条件的int的惰性集合。 client_id.First() will give you the first id in that collection. client_id.First()将为您提供该集合中的第一个ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM