Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

c#, linq to sql

I just want return class library - Domain.User
I assume I can use uTmp as class to return but I do not know how to do.


public Domain.User ReadCommonUserInfo(string userID)
        {
            Domain.User uTmp = new Domain.User();
            Repository.UserDataContext ldc = new Repository.UserDataContext();
            var userInfo = from x in ldc.TblUsers where x.UserID == userID
                        select new Domain.User
                        {
                          uTmp.email = x.Email,
                          uTmp.password = x.Password,
                    uTmp.userType = x.UserType,
                          uTmp.status = x.Status,
                    uTmp.firstName = x.FirstName,
                          uTmp.lastName = x.LastName
                             };
            return u;
        }      




 public class User
    {
        [Display(Name = "UserID")]public string userID { get; set; }
        [Display(Name = "Email")]public string email { get; set; }
        [Display(Name = "Password")]public string password { get; set; }
        [Display(Name = "UserType")]public string userType { get; set; }
        [Display(Name = "Status")]public string status { get; set; }
        [Display(Name = "RoleName")]public string roleName { get; set; }
        [Display(Name = "Description")]public string roleNameDescription { get; set; }
        [Display(Name = "FirstName")]public string firstName { get; set; }
        [Display(Name = "LastName")]public string lastName { get; set; }


    }
Avatar of it_saige
it_saige
Flag of United States of America image

If you are wanting to return the first instance you find, then you can simply do something like:
public Domain.User ReadCommonUserInfo(string userID)
{
	Repository.UserDataContext ldc = new Repository.UserDataContext();
	return (from x in ldc.TblUsers
		   where x.UserID.Equals(userID, StringComparison.OrdinalIgnoreCase)
		   select new Domain.User()
		   {
			   email = x.Email,
			   password = x.Password,
			   userType = x.UserType,
			   status = x.Status,
			   firstName = x.FirstName,
			   lastName = x.LastName
		   }).FirstOrDefault();
}

Open in new window

Ofcourse if TblUsers is already of the Domain.User type, then you don't need to select a new one, just select the first or default; e.g. -
public Domain.User ReadCommonUserInfo(string userID)
{
	Repository.UserDataContext ldc = new Repository.UserDataContext();
	return (from x in ldc.TblUsers
		   where x.UserID.Equals(userID, StringComparison.OrdinalIgnoreCase)
		   select x).FirstOrDefault();
}

Open in new window

-saige-
Avatar of ITsolutionWizard

ASKER

what if the search is null? will return object be generalized error?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
// Paul was not found, should we add him?
on the front end - usually we will just show them a label saying 'not found'