如何从表中选择第一个id [英] How to Select the first id from table

查看:76
本文介绍了如何从表中选择第一个id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的子类别列出所有条目。所以我想在加载主页上显示条目列表而不事先选择任何类别或子类别。



我做了一个返回条目列表的查询,但手动放置subCategoryId代替preQuery。



以下是查询:



I want to list all the entries depending on my subcategory. So i want to display the entries list on load home page without previous selecting any categories or subcategories.

Ive made a query that is returning list of entries, but putting manually subCategoryId in the place of preQuery.

Here is the query:

int userId = WebSecurity.GetUserId(User.Identity.Name);

 var preQuery = (from sub in dc.SubCategory
                 join c in dc.Category on sub.SubCategoryId equals c.Id
                 join u in dc.User on c.UserId equals u.UserId
                 where u.UserId == userId
                 select sub.SubCategoryId).Take(1).FirstOrDefault();

       var query = (from e in dc.Entry
                    join sub in dc.SubCategory on e.SubCategoryId equals sub.SubCategoryId
                    join cat in dc.Category on sub.CategoryId equals cat.Id
                    join u in dc.User on cat.UserId equals u.UserId
                    where ((u.UserId == userId) && (cat.UserId == userId)
                        && (sub.CategoryId == cat.Id) && (e.SubCategoryId == preQuery ))
                    select e).ToList();

       return View(query);





所以我的观点是我想要而不是手动编写subCategoryId为6,想要获取子类别ID列表并从中选择第一个ID。我已经预先查询但仍然不是必需的结果。任何建议我做错了什么?



So my point is that i want instead of writing manually subCategoryId of 6, want to take the list of subcategories ids and select the first id from it. I've made prequery but still not the required result.Any suggestions what i am doing wrong?

推荐答案

如果你想从<$ c $中选择第一个c>查询变量,那么你应该知道,它是一个List项。您可以使用此代码,



If you want to select the first from the query variable, then you should know, it is a List item. You can use this code,

var first = query.First();





这将为您提供第一项。如果List是相反的,你可以选择Last。



This would give you the first item. If the List is in opposite way, you can select Last.


你无法组合查询 - 你有这个



couldn't you combine the query - you have this

var query = (from e in dc.Entry
                     join sub in dc.SubCategory on e.SubCategoryId equals sub.SubCategoryId
                     join cat in dc.Category on sub.CategoryId equals cat.Id
                     join u in dc.User on cat.UserId equals u.UserId
                     where ((u.UserId == userId) && (cat.UserId == userId)
                         && (sub.CategoryId == cat.Id) && (e.SubCategoryId == preQuery ))
                     select e).ToList();





你能不能简单地嵌套这两个,即用'
取代'prequery'



can you not simply nest the two, ie replace 'prequery' with

(from sub in dc.SubCategory
                  join c in dc.Category on sub.SubCategoryId equals c.Id
                  join u in dc.User on c.UserId equals u.UserId
                  where u.UserId == userId
                  select sub.SubCategoryId).Take(1).FirstOrDefault())





获得





to get

(from e in dc.Entry
                     join sub in dc.SubCategory on e.SubCategoryId equals sub.SubCategoryId
                     join cat in dc.Category on sub.CategoryId equals cat.Id
                     join u in dc.User on cat.UserId equals u.UserId
                     where ((u.UserId == userId) && (cat.UserId == userId)
                         && (sub.CategoryId == cat.Id) && (e.SubCategoryId == (from sub in dc.SubCategory
                  join c in dc.Category on sub.SubCategoryId equals c.Id
                  join u in dc.User on c.UserId equals u.UserId
                  where u.UserId == userId
                  select sub.SubCategoryId).Take(1).FirstOrDefault() ))
                     select e).ToList();





(我只是做了剪切和粘贴 - 你必须整理括号)



(Ive just done a cut and paste - you'd have to tidy up the brackets)


这是我所做的解决方案。如果有人建议如何优化解决方案,欢迎分享。



This is the solution that i have made. If anyone has suggestion how to optimize the solution, is welcome to share.

int userId = WebSecurity.GetUserId(User.Identity.Name);

            var preQuery = (from e in dc.Entry
                         join sub in dc.SubCategory on e.SubCategoryId equals sub.SubCategoryId
                         join cat in dc.Category on sub.CategoryId equals cat.Id
                         join u in dc.User on cat.UserId equals u.UserId
                         where u.UserId == userId
                         select e.SubCategoryId).Take(1);

            var query = (from e in dc.Entry
                         join sub in dc.SubCategory on e.SubCategoryId equals sub.SubCategoryId
                         join cat in dc.Category on sub.CategoryId equals cat.Id
                         join u in dc.User on cat.UserId equals u.UserId
                         where ((u.UserId == userId) && (cat.UserId == userId)
                             && (sub.CategoryId == cat.Id) && (e.SubCategoryId == preQuery.FirstOrDefault()))
                         select e).ToList();

            return View(query);


这篇关于如何从表中选择第一个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆