1

Hi im trying to assign a default group to any user that registers with my site. Since the admin Id of that group is the administrator, I query my database for a group with a admin id of my administrator and then assign that group to the user. However the following error is being thrown "Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[DatabaseModel1.Group]' to type 'DatabaseModel1.Group'. " Heres the code

        Dim defaultGroup = (From group As Group In context.Groups
                     Where group.AdminID = ((From users As User0 In context.User0
                                            Where users.Name Like "Administrator"
                                            Select users.UserID).First)
                       Select group)

        currentUser.Groups.Add(defaultGroup)//the error is being thrown here

any help would be appreciated Thanks

2 Answers 2

5

It sounds like you've got a query which can return multiple results, but you're trying to assign it to a single-value variable.

You probably just need to use something like:

currentUser.Groups.Add(defaultGroup.First)

The probable options are:

  • First() - allows multiple results and returns the first; will throw an exception if there are none
  • FirstOrDefault() - allows multiple results and returns the first; will return the default value for the element type (e.g. null) if there are no results
  • Last() - allows multiple results and returns the last; will throw an exception if there are none
  • LastOrDefault() - allows multiple results and returns the last; will return the default value for the element type (e.g. null) if there are no results
  • Single() - expects exactly one result; if there are no results or mulitple results, an exception is thrown

Now that you've shown the query, it sounds like you should probably use a join rather than a nested query. I'm not hot on the VB query syntax, but in C# you might want:

var defaultGroupQuery = 
        from group in context.Groups
        join user in context.User0 on group.AdminID equals user.UserID
        where user.Name == "Administrator"
        select group;
Sign up to request clarification or add additional context in comments.

1 Comment

Damn... can't believe I fell for this for a second time. Thanks Jon!
0
    List<int> adminIDs = context.User0.Where(u=> u.Name.Contains("Administrator"))
                               .Select(u=>u.UserId);

    //you can use `FirstOrDefault()` above and it will return only int not `List<int>`
    // but i'm showing you how to get all IDs then you can get the first one

    int adminId = adminIDs.FirstOrDefault();

    //and then get only one Group using this adminID
    var defaultGroup = context.Groups.FirstOrDefault(g=>g.AdminID == adminId);
    if(defaultGroup != null)
    {
      currentUser.Groups.Add(defaultGroup);
    }

    //you can use the `List<int> adminIDs` if you need to get all admin groups

var groups = context.Groups.Where(gg => adminIDs.Contains(gg.AdminID));

Hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.