views:

44

answers:

3
tblUserRole Permission = 
      (oCurrenUserPermission.GetPermission(Convert.ToString(Session["Navigation"])));
if (Permission.IsInsert == 1)
{
}

public IQueryable GetPermission(string sPageName)
{
   IQueryable query;
   #region MyRegion
   query = from r in this.Context.tblUserRoles
              join p in this.Context.tblPageInfos on r.PageID equals p.PageID
             where r.Record_Status == 2 && p.PageName == sPageName
           select r;
   return query;
  #endregion
 }

Above syntax show the bellow error:

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'AkijBeverage.ServiceObject.tblUserRole'. An explicit conversion exists (are you missing a cast?) E:\Project-Akij\09-July-2010\AkijBeverage\AkijBeverage\SecurityUserControls\UCUserRole.ascx.cs 63 43 AkijBeverage

How to solve this ?

A: 

The problem is you have an IQueryable and are attempting to assign to a "Type" without a cast.

Either change your method to:

  public IQueryable<tblUserRole > GetPermission(string sPageName)

and then do (First/FirstOrDefault/Single/etc..)

 oCurrenUserPermission.GetPermission(...).FirstOrDefault();

Or cast it and cross your fingers and select one.

   (tblUserRole)oCurrenUserPermission.GetPermission(...).FirstOrDefault();

Side Note

I would suggest you work on naming your code in a more friendly maner. No need to prepend the "type" at the beginning. For example a possible better way to name everything:

 string navigationValue = Convert.ToString(Session["Navigation"]);

 UserRole permission = 
  currentUserPermission.GetPermission(navigationValue ).FirstOrDefault();

 //If this isn't a boolean i would consider using one
 if (permission.IsInsert == 1)
 {
 }

 public IQueryable<UserRole> GetPermission(string sPageName)
 {

    #region MyRegion
    var query = from r in this.Context.UserRoles
                    join p in this.Context.PageInformation on r.PageID equals p.PageID
                where r.Record_Status == 2 && p.PageName == sPageName
                select r;
   return query;
   #endregion
 }
Nix
using .AsQueryable(),error remains
shamim
+1  A: 

As far as I can tell, that query is just not compatible with tblUserRole.

It's going to return a IEnumereable<bool> instead of a tblUserRole. The real problem is that you are selecting the IsInsert in two different spots: In the query, and again in the if()

The easiest way would be just return the full tblUserRole object:

return (from r in this.Context.tblUserRoles 
                join p in this.Context.tblPageInfos on r.PageID equals p.PageID 
                where r.Record_Status == 2 && p.PageName == sPageName 
                select r).SingleOrDefault();
James Curran
A: 

Currently you're returning a query - not a single item. What do you want to return?

Here's one option, for example:

public tblUserRole GetPermission(string sPageName)
{
    return (from r in this.Context.tblUserRoles
            join p in this.Context.tblPageInfos on r.PageID equals p.PageID
            where r.Record_Status == 2 && p.PageName == sPageName
            select r).FirstOrDefault();
}

This will return the first matching role, or null if there aren't any.

If you do want to return a query, it would be better as a strongly typed IQueryable<T>:

public IQueryable<tblUserRole> GetPermissions(string sPageName)
{
    return from r in this.Context.tblUserRoles
           join p in this.Context.tblPageInfos on r.PageID equals p.PageID
           where r.Record_Status == 2 && p.PageName == sPageName
           select r;
}

Then you'd need to change your calling code to something like this:

tblUserRole Permission 
   oCurrenUserPermission.GetPermissions(Convert.ToString(Session["Navigation"]))
                        .FirstOrDefault();
Jon Skeet