0

I need a way to get matching records of the ProcessedBy column and user.Id (which in db share exact same string but in different tables).

Short: 'user' is an instance of the ApplicationUser Class and ProcessedBy is a property in LI class of type ApplicationUser. Problem: If I apply ProcessedBy.ToString() there will be a error "Cannot convert ApplicationUser to string". ProcessedBy is a property of class ApplicationUser which contains Id directly, user is an object of class ApplicationUser which contains Id as a string property, not my design I need to figure this out.

public static void UpdateFilesProcessedByUser(ApplicationUser user)
{
    ApplicationDbContext db = new ApplicationDbContext();

    //Cannot convert ApplicationUser ProcessedBy to string, user.Id is a string
    IQueryableL<LI> DbQueryGet = db.LIs.Where(l =>
        l.ProcessedBy.ToString() == user.Id);  

    var GetNumberOfFiles = DbQueryGet.ToList().Count();
    IQueryable<ApplicationUser> DbQueryUpdate = db.Users.Where(u => u.Id == user.Id);
    var GetUser = DbQueryUpdate.ToList();
    GetUser[0].FilesProcessed = GetNumberOfFiles;
    db.SaveChanges();
}

And LI class which contains ProcessedBy :

[Table("LIs")]
public class LI
{
  .
  .
  .
  public virtual ApplicationUser ProcessedBy {get; set;}
}
  • Are `ProcessedBy` and `user.Id` guids? – Poosh Jun 05 '17 at 01:01
  • What types are `ProcessedBy` and `Id`? – Rufus L Jun 05 '17 at 01:15
  • Idk, I can't see implementation of Identity Framework, but for sure Id property of user object can be accessed as a string. 'user' is an instance of the ApplicationUser Class and ProcessedBy is a property in LetterInfo class of type ApplicationUser. –  Jun 05 '17 at 01:15
  • What do you see when you right-click on `ProcessedBy` and select "Go To Definition"? (Same with `Id`)? That should show you the type of the object from the metadata. – Rufus L Jun 05 '17 at 01:17
  • ProcessedBy => ApplicationUser LetterInfo.ProcessedBy. user => ApplicationUser ; Id => string –  Jun 05 '17 at 01:23

1 Answers1

0

Here is the solution , if i have understood your problem

I hope as a first thing , you want to convert the Applicationuser.id to the string ,by default Applicationuser.id is the guid id .here is the reference link:https://msdn.microsoft.com/en-us/magazine/dn818488.aspx?f=255&MSPPError=-2147217396

So to convert the guid to string ,here is the example:

Guid userid = user.Id;
string usrid = userid.ToString();

Addition reference:https://www.codeproject.com/Questions/124616/converting-guid-to-string-and-vice-versa

Addition Info:

Below is the another way of how you can easily take the user id and user object

var user = UserManager.FindById(User.Identity.GetUserId());

Outside controller:

System.Web.HttpContext.Current.User.Identity.GetUserId();

Useful reference: https://stackoverflow.com/a/22835540/3397630

Hope the above information was useful.

Thanks Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12