I am using the following code in C#
var users = (from user in twitterCtx1.User
where user.Type == UserType.Lookup &&
user.UserID == list1
select user)
.ToList();
The list1 has all the IDs of the verified accounts of twitter and I am processing 75 records at a time. when I debug my code, I see that the list1 is populated with all the IDs but when the control passes in this portion of the code, it does not enter inside the it as the value being passed here is NULL
I am not able to understand as to why the value is NULL. What am I missing here?
Thanks in advance!
You're comparing user.UserID to the entire list1 object. Did you mean to write list1.Contains(user.userID)?
I'm still not clear on what error you're seeing, but here are some tips that might help.
list1 needs to be a string that is a comma-separated list of user IDS. Here's some code to show how it works:
var followers =
(from user in twitterCtx.SocialGraph
where user.Type == SocialGraphType.Followers &&
user.ScreenName == "JoeMayo"
select user)
.SingleOrDefault();
var userIDs = string.Join(",", followers.IDs.Take(100).ToList());
var users =
(from user in twitterCtx.User
where user.Type == UserType.Lookup &&
user.UserID == userIDs
select user)
.ToList();
First, the demo gets a list of user IDs. It's a user object with an IDs collection of type ulong. The next statement creates a comma-separated string of 100 of those IDs. Finally the lookup assigns that comma-separated string to the UserID property.
You should examine list1 and verify that it's a properly formatted comma-separated string of user IDs. The max size is 100.
Related
I have the following Book table:
From this table, I am trying to get the latest registrationNumber based on the group ID as an input from the user.
So, my query looks like this at the moment:
var booksQuery = _context.Books.Where(g => g.GroupId == id)
.OrderByDescending(g => g.RegistrationNumber).GroupBy(g => g.GroupId);
id is the group Id specified by the user. So for example, if id = 15, then I should get the 15:6 as my latest registration number. To do that, I basically grouped by id and ordered the result by descending order. But that is giving me null results. Anyone know why? I am very new to this LINQ-Entitiy coding.
As mentioned by others you really should make your registrationNumber field an integer since you are wanting to sort on it. In the event, you can't make the change, below is a Linq query that basically parses the registration number and converts to an integer to sort on the first and second part by splitting at the colon. This works for sorting when you have 15:10, etc, as in the string sort 15:6 comes before 15:10
var booksQuery = books.Where(g => g.GroupId == id).ToList();
var bookWanted = booksQuery
.OrderByDescending(g => int.Parse(g.registrationNumber.Split(':')[0]))
.ThenByDescending(g=> int.Parse(g.registrationNumber.Split(':')[1]))
.FirstOrDefault();
I am using ASP .NET Identity 2.0 as my user manager. Right now I want to retrieve the list of all users with RoleId different from "4". My LINQ query looks like this:
var userList = from entry in this.UserManager.Users
from roles in entry.Roles
where roles.RoleId != "4"
group entry by entry.Id into g
select new { g.Id, g.UserName, g.Email, g.EmailConfirmed, g.PhoneNumber, g.PhoneNumberConfirmed, g.Roles };
Unfortunately, group by completely doesn't work and g's parameters are not recognized. Without grouping the list contains separate rows for every user->role and it is not my desired effect. What is wrong in my query?
Edit:
Of course each user has a number of roles. If one of them is RoleId == 4 then I do not want his record to be retrieved.
I don't see any need for grouping. Try this much simpler query:
var userList = this.UserManager.Users
.Where(u => u.Roles.All(r => r.Id != "4"));
This should give you the result you are looking for.
var userList = (from entry in this.UserManager.Users
//group entry by entry.Id into g //I don't know why you need to group this,usually `id` is primary key
select new { entry.Id, entry.UserName, entry.Email, entry.EmailConfirmed, entry.PhoneNumber,
entry.PhoneNumberConfirmed, Roles=entry.Roles.Where(r=>r.RoleId != "4").ToList() })
.Where(g=>g.Roles.Count>0);
if there is no role's in result, the user will be removed
Above you can see FollowingUsers and StatusUpdates tables.
In FollowingUsers, I store Follower's Username and Following's Username.
In StatusUpdates, I store Status updates of users.
Below you can see original query I wrote to retrieve status updates of user who logged in.
var list = new List<StatusUpdate>();
list = (from x in db.StatusUpdates
where x.Author == User.Identity.Name
orderby x.Timestamp descending
select x)
.Take(count).ToList();
How to get status updates from followings of logged in user?
The following should work, although I don't have your database to test it on. Note that it won't actually be executed until the call to ToList so everything should still happen in a single database query. Also, the creation of a new list is not needed as it will be overwritten by your query so I've tidied that up a little.
var Users = from f in db.FollowingUsers
where f.FollowerId == User.Identity.Name
select f.FollowingId;
var list = (from x in db.StatusUpdates
from y in Users
where x.Author == y
orderby x.Timestamp descending
select x)
.Take(count).ToList();
I have a table Users. Users has a column rating. How i can get information about user place using linq2sql? I want method like:
var userPlace =
GetUserPlaceById(userId);
Table Users may contains a few thousands users.
Sorry guys. Users DOESNT contain place column. Real example: Rating is chess elo rating. if you have high rating then you on 1st place. If you have lower rating then you on the last place.
Did you mean something like this?
int userRating = users.Single(user => user.Id = userId).Rating;
int userPlace = users.Where(user => user.Rating < userRating).Count() + 1;
I have a table Users. Users has a column rating. How i can get information about user place using linq2sql?
I'm not sure what "userPlace" is, but assuming it is a column in that table...
var userPlace = (from user in db.Users
where user.Id == userId
select user)
.First()
.UserPlace;
Be aware that calling .First() will throw an exception if no match is found, so if you expect that sometimes this user will not exist use FirstOrDefault, check for null, and then grab the UserPlace property.
You would use something like:
string GetUserPlaceById(int userId)
{
IQueryable<User> users = GetUsers(); // Get users queryable reference
return users.Single(user => user.Id == userId).Place;
}
You could do something like this:
var userPlace = _db.Users.Where(x => x.UserId == userId).Select(x => x.Place).SingleOrDefault();
I have a table of Users and a one to many UserSkills table. I need to be able to search for users based on skills. This query takes a list of desired skills and searches for users who have those skills. I want to sort the users based on the number of desired skills they posses. So if a users only has 1 of 3 desired skills he will be further down the list than the user who has 3 of 3 desired skills.
I start with my comma separated list of skill IDs that are being searched for:
List<short> searchedSkillsRaw = skills.Value.Split(',').Select(i => short.Parse(i)).ToList();
I then filter out only the types of users that are searchable:
List<User> users = (from u in db.Users
where
u.Verified == true &&
u.Level > 0 &&
u.Type == 1 &&
(u.UserDetail.City == city.SelectedValue || u.UserDetail.City == null)
select u).ToList();
and then comes the crazy part:
var fUsers = from u in users
select new
{
u.Id,
u.FirstName,
u.LastName,
u.UserName,
UserPhone = u.UserDetail.Phone,
UserSkills = (from uskills in u.UserSkills
join skillsJoin in configSkills on uskills.SkillId equals skillsJoin.ValueIdInt into tempSkills
from skillsJoin in tempSkills.DefaultIfEmpty()
where uskills.UserId == u.Id
select new
{
SkillId = uskills.SkillId,
SkillName = skillsJoin.Name,
SkillNameFound = searchedSkillsRaw.Contains(uskills.SkillId)
}),
UserSkillsFound = (from uskills in u.UserSkills
where uskills.UserId == u.Id && searchedSkillsRaw.Contains(uskills.SkillId)
select uskills.UserId).Count()
} into userResults
where userResults.UserSkillsFound > 0
orderby userResults.UserSkillsFound descending
select userResults;
and this works! But it seems super bloated and inefficient to me. Especially the secondary part that counts the number of skills found.
Thanks for any advice you can give.
--r
I think that should do the trick:
(from u in users
where u.UserSkills.Any(skill => searchedSkillsRaw.Contains(skill.SkillId))
select new
{
u.Id,
u.FirstName,
u.LastName,
u.UserName,
UserPhone = u.UserDetail.Phone,
UserSkills = u.UserSkills,
UserSkillsFound = u.UserSkills.Where(skill => searchedSkillsRaw.Contains(skill.SkillId)).Count()
} into userResults
orderby userResults.UserSkillsFound descending
select userResult).ToList();
However, since this is a query that gets executed on SQL server I strongly recommend to remove the 'ToList()' call from the first query. Because that actually causes LINQ to run two separate queries on the SQL server. You should change it to IQueryable instead. The power of LINQ is to construct queries in several steps without having to actually execute it in between. So 'ToList' should be called only at the end when the entire query has been constructed. In fact what you currently do is running the second query in memory rather than on the database server.
In regards to your UserSkills one-to-many relation you do not need to do an explicity join in LINQ. You can just access the collection property instead.
Let me know if you need more explanation.
Michael
Why not just let people do, say, fUsers.UserSkills.Count()? It would reduce the amount of data retrieved from the server in the first place.
Alternatively, you could create a View that has a calculated field in it and then map that to a type. Would push the query for count down into the DB.