LINQ 问题 - 如何使 where != 值

发布于 2024-12-04 09:46:55 字数 630 浏览 0 评论 0原文

我有一个“朋友”表,其中包含登录用户的 ID 及其朋友的 ID。

我用这个 SQL 查询得到的结果

SELECT u.ID, u.Nickname 
FROM UserSet as u, FriendsSet as f
WHERE u.ID=f.FriendID
ORDER BY u.Nickname

| ID | Nickname |
-----------------
| 16 | rugardini|
| 17 |   Teste  |

“rugardini”是我当前的用户,这似乎是因为在友谊关系中“Teste”是我的朋友,我也是他的朋友,但在我的朋友列表中我不希望我的出现自己的用户。

所以我做了以下查询,不包括我自己的 ID (16)

SELECT u.ID, u.Nickname 
FROM UserSet as u, FriendsSet as f
WHERE u.ID=f.FriendID and u.ID != 16
ORDER BY u.Nickname

我有 2 个问题:

1) 这是进行此选择的最佳方式吗? 2) 如何将此 SQL 查询转换为 LINQ?

I have a table "Friends" with the ID of the logged user and his/her friend's IDs.

The result I get with this SQL query

SELECT u.ID, u.Nickname 
FROM UserSet as u, FriendsSet as f
WHERE u.ID=f.FriendID
ORDER BY u.Nickname

Is

| ID | Nickname |
-----------------
| 16 | rugardini|
| 17 |   Teste  |

"rugardini" is my current user, it appears because in a friendship relation "Teste" is my friend and I'm his friend too, but in my list of friends I don't want my own user appears.

So I did the following query excluding my own ID (16)

SELECT u.ID, u.Nickname 
FROM UserSet as u, FriendsSet as f
WHERE u.ID=f.FriendID and u.ID != 16
ORDER BY u.Nickname

I have 2 questions:

1) Is this the best way of doing this selection?
2) How to convert this SQL query to LINQ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归途 2024-12-11 09:46:55

关于 2):第二个查询的 linq 应该如下所示:

from u in db.UserSet, f in db.FieldsSet
where u.ID=f.FriendID && u.ID != 16
orderby u.Nickname
select u.ID, u.Nickname 

about 2): linq for your second query should look like this:

from u in db.UserSet, f in db.FieldsSet
where u.ID=f.FriendID && u.ID != 16
orderby u.Nickname
select u.ID, u.Nickname 
允世 2024-12-11 09:46:55

我只能像这样执行我的查询:

var friends = (from u in db.UserSets
               join f in db.FriendsSets
               on u.ID equals f.FriendID
               where u.ID != userID
               orderby u.Nickname
               select new Friends { ID = u.ID, Nickname = u.Nickname }).ToList();

在我的控制器中使用这样的类:

public class Friends
{
    public int ID;
    public string Nickname;
}

在我的视图中像这样循环:

<ul>
@foreach (dynamic item in ViewBag.Friends)
{
    <li>@item.Nickname @Html.ActionLink("Amigo", "Index", new { id = item.ID })</li>
}
</ul>

感谢所有回答我问题的人=)

I could perform my query only like this:

var friends = (from u in db.UserSets
               join f in db.FriendsSets
               on u.ID equals f.FriendID
               where u.ID != userID
               orderby u.Nickname
               select new Friends { ID = u.ID, Nickname = u.Nickname }).ToList();

With a class in my controller like this:

public class Friends
{
    public int ID;
    public string Nickname;
}

And a loop in my view like this:

<ul>
@foreach (dynamic item in ViewBag.Friends)
{
    <li>@item.Nickname @Html.ActionLink("Amigo", "Index", new { id = item.ID })</li>
}
</ul>

Thank you for all those who answered my question =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文