how do I get the value instead of count?

Nirmala

Member
Joined
Jan 30, 2022
Messages
10
Programming Experience
Beginner
C#:
var or = await _context
.OrganisationID
.FromSqlRaw("SELECT pj.proj_org_id as orgId FROM[mapping].[DataResourceTenancy] as dt JOIN web.Project as pj on dt.drp_projCode = proj_code where drp_amrd_serialNumber like {0} ", deviceName)
.ToListAsync();


When I execute, it is giving me the count not the value. Please help me
 
Last edited by a moderator:
Moving to Entity Framework...
 
You are seeing a Count in the debugger panes because that one of the default properties displayed by a list, because you asked for a list byt calling ToListAsync(). You need to iterate over the list to get the actual objects. Or if you know that there is only ever one object, you could use Single() instead of ToList().
 
You are seeing a Count in the debugger panes because that one of the default properties displayed by a list, because you asked for a list byt calling ToListAsync(). You need to iterate over the list to get the actual objects. Or if you know that there is only ever one object, you could use Single() instead of ToList().
Thank you for your response, will try changing to that and get back to you
 
You are seeing a Count in the debugger panes because that one of the default properties displayed by a list, because you asked for a list byt calling ToListAsync(). You need to iterate over the list to get the actual objects. Or if you know that there is only ever one object, you could use Single() instead of ToList().
When I change it to .Single(); its giving

Compiler Error CS1061 : 'type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?).​

the code is like this:
C#:
public async Task<int> GetOrgIdByDeviceName(string deviceName)
         {
             var or = await _context
                            .GetOrganisationID
                            .FromSqlRaw("SELECT OrgId = [dbo].[FcGetOrgIdByDeviceName]({0}) ", deviceName)
                            .Single();
            return or;
         }
 
Last edited by a moderator:
Are you sure the error is there? The error is talking about a variable name, but there is no such variable there in the code you presented. The variable there is deviceName.
 
Are you sure the error is there? The error is talking about a variable name, but there is no such variable there in the code you presented. The variable there is deviceName.
1643838292592.png

This is what it says when I hover on FromSqlRaw. Can someone explain me CS1061 error meaning please
 
*sigh* The issue is that you are trying to await on the returned single object. Since that object has no awaiter because it is not a Task, you get that compile time error which tells you that it can't find the GetAwaiter() method.

The quickest path forward for you is to remove the async/await:
C#:
public int GetOrgIdByDeviceName(string deviceName)
{
    return _context
           .GetOrganisationID
           .FromSqlRaw("SELECT OrgId = [dbo].[FcGetOrgIdByDeviceName]({0}) ", deviceName)
           .Single();
}

If you really much have async functionality, then you need to use part of your original code to get the list asynchronously, and then once you have the list, just pull the single object from it.
C#:
public async Task<int> GetOrgIdByDeviceNameAsync(string deviceName)
{
    var list = await _context
               .GetOrganisationID
               .FromSqlRaw("SELECT OrgId = [dbo].[FcGetOrgIdByDeviceName]({0}) ", deviceName)
               .ToListAsync();
    return list.Single();
}
 
*sigh* The issue is that you are trying to await on the returned single object. Since that object has no awaiter because it is not a Task, you get that compile time error which tells you that it can't find the GetAwaiter() method.

The quickest path forward for you is to remove the async/await:
C#:
public int GetOrgIdByDeviceName(string deviceName)
{
    return _context
           .GetOrganisationID
           .FromSqlRaw("SELECT OrgId = [dbo].[FcGetOrgIdByDeviceName]({0}) ", deviceName)
           .Single();
}

If you really much have async functionality, then you need to use part of your original code to get the list asynchronously, and then once you have the list, just pull the single object from it.
C#:
public async Task<int> GetOrgIdByDeviceNameAsync(string deviceName)
{
    var list = await _context
               .GetOrganisationID
               .FromSqlRaw("SELECT OrgId = [dbo].[FcGetOrgIdByDeviceName]({0}) ", deviceName)
               .ToListAsync();
    return list.Single();
}
Thank you so much for your help. Can you also tell me how to convert the string value to int as the return type is int?
 
I recommend using int.TryParse() for production code. For quick and dirty code where you are just trying to prototype stuff, use int.Parse() or Convert.ToInt32().
 
It should be pretty self-explanatory. You are trying to edit code that is currently running. Stop debugging. Edit the code. Recompile. The run with the debugger again.

I'm puzzled why you would need to convert a string to an integer when you already have an integer. The compiler would not have let you start debugging code if your method returned a Task<int> or int[/code] if the result of calling [icode].Single() was not an int.
 
It should be pretty self-explanatory. You are trying to edit code that is currently running. Stop debugging. Edit the code. Recompile. The run with the debugger again.
I realized that and stopped debugging, then I recompiled. But the error is CS1501 : no overload for method 'TryParse'' takes 1 arguments
 
The compiler is correct. You should read the documentation sometime before posting a question. Part of being a developer is learning how to read documentation.
 
It should be pretty self-explanatory. You are trying to edit code that is currently running. Stop debugging. Edit the code. Recompile. The run with the debugger again.

I'm puzzled why you would need to convert a string to an integer when you already have an integer. The compiler would not have let you start debugging code if your method returned a Task<int> or int[/code] if the result of calling [icode].Single() was not an int.
1643843922125.png

this is the reason I'm trying to convert to int
 
Back
Top Bottom