user3568783
user3568783

Reputation:

How can I restrict a LINQ Query?

What I would like to do is to find the first or default value of the role Name given the Id.

Here is the LINQ I tried:

var roleId = await db.AspNetRoles
                     .Select(r => r.Id)
                     .Where(r => r.)
                     .FirstOrDefault();

Here is my class:

I have this class in Entity Framework Asp.Net Identity

public AspNetRole()
{
    this.AspNetUsers = new List<AspNetUser>();
}

public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<AspNetUser> AspNetUsers { get; set; }

When I look at what options I have after the r. I find that I cannot put in a name like r.Name

Can someone show me what I am doing wrong?

Upvotes: 0

Views: 252

Answers (6)

CodeCaster
CodeCaster

Reputation: 151740

Because the Select projects the source onto a new mapping, in this case only containing the int Id. From there on you'll be operating on an IEnumerable<int>, which don't have a Name property.

You could use:

.Select(r => new { r.Id, r.Name })

To project into an anonymous object only containing Id and Name.

Alternatively you can query first, then project:

await db.AspNetRoles
        .Where(r => r.Id == someId)
        .Select(r => r.Name)    
        .FirstOrDefault();

Or omit the projection (the .Select() call) entirely, but it all depends on what you want to do with the results.

Upvotes: 4

Rahul Singh
Rahul Singh

Reputation: 21825

Try this:-

var roleId = await db.AspNetRoles
                  .Where(r => r.Id == YourId)
                  .Select(x => x.Name)
                  .FirstOrDefault();

Or (If you are sure You have that Id):-

var roleId = await db.AspNetRoles.FirstOrDefault(x => x.Id == YourId).Name;

But, Please note it will throw Null Reference Exception if no id matches :)

Upvotes: 1

Andrei
Andrei

Reputation: 341

Your issue makes perfect sense:). The problem is you are projecting before filtering. After the select you'll get a collection of ints. What you need to do is revers the query like so :

  var roleId = await db.AspNetRoles
       .Where(r => r.Name=%smething%)
       .Select(r => r.Id)           
       .FirstOrDefault();

I hope this helps :)

Upvotes: 1

t3chb0t
t3chb0t

Reputation: 18754

You select r.Id in your select so the result only contains the Ids. You could also do it like this:

var role = await db.AspNetRoles.FirstOrDefault(r => r.Name == "Some name");
if (role != null)
{ 
    // more code about role.Id
}

Upvotes: 0

Steve Lillis
Steve Lillis

Reputation: 3256

Just change the order like so:

var roleId = await db.AspNetRoles
                      .Where(r => r.Id == 24)
                      .Select(s => s.Name)
                      .FirstOrDefault():

Upvotes: 0

Michael Mairegger
Michael Mairegger

Reputation: 7301

You have to Change the order of the execution to

var roleId = await db.AspNetRoles
       .Where(r => r.Name = "your Name")
       .Select(r => r.Id)
       .FirstOrDefault();

The Problem in your code is that you Select the Id which is of type int. Then when you call Where you have a list of ints available. If you swap where and select you first filter on AspNetRoles then select the int.

Upvotes: 1

Related Questions