0

I have two database table called employee and employee detail. and i use entityframwork.

i want to make a edit page which contains both table fields. Now when i click on grid view (having detail of both the table) i passed primary id and make a join based on it.

join detail is below

var list = (from g in db.Employee
                   join d in db.db.EmployeeDetail on g.EGID equals d.EGID
                   where g.EGID == id
                   select new CombineModel
                     {
                          EE_GENERAL = g,
                          EE_DEMOGRAPHIC = d
                     });

on above code "CombineModel" is class contain property with two table fields.

public class CombineModel
{
    public Employee EE_GENERAL { get; set; }
    public EmployeeDetail EE_DEMOGRAPHIC { get; set; }

}

now when i return a view of my edit page by passing "list" it shows me error and i unable to get data of my join. my edit view is bind with combinemodel.

Please guide me how i can type cast above list or get detail of list in view.

Below is Employee Class

 [MetadataType(typeof(Employee_Validation))]
public partial class Employee
{

}

public class Employee_Validation
{
    public Decimal EGID { get; set; }
    public Decimal CID { get; set; }

    [StringLength(20)]
    //[Required(ErrorMessage = "Employee ID is required")]
    public String EmployeeID { get; set; }

    [Required(ErrorMessage = "Effective Date is required")]
    [DataType(DataType.Date)]
    public DateTime EffectiveDate { get; set; }

    public DateTime AddDate { get; set; }

    public String AddOper { get; set; }

    public DateTime ChgDate { get; set; }

    public String ChgOper { get; set; }

    [StringLength(30)]
    [Required(ErrorMessage = "Last Name is required")]
    public String LastName { get; set; }

    [StringLength(30)]
    [Required(ErrorMessage = "First Name is required")]
    public String FirstName { get; set; }


    [StringLength(15)]
    public String MiddleName { get; set; }

    [StringLength(50)]
    public String CheckName { get; set; }

    public Int32 SSNO { get; set; }

    [Required(ErrorMessage = "Please select atleast one security group.")]
    public String SecurityGroup { get; set; }

    [Required(ErrorMessage = "Please select atleast one security level.")]
    public Byte SecurityLevel { get; set; }

    public Byte EESequence { get; set; }

    [Required(ErrorMessage = "Change Type is required")]
    public Byte ChangeType { get; set; }

    [StringLength(200)]
    public String OptionalComments { get; set; }

}

Below is EmployeeDetail class

 [MetadataType(typeof(EmployeeDetail_Validation))]
public partial class EmployeeDetail
{ 

}

public class EmployeeDetail_Validation
{

    public Decimal DemographicId { get; set; }
    public Decimal CID { get; set; }
    public Decimal EGID { get; set; }

    public DateTime EffectiveDate { get; set; }
    public DateTime AddDate { get; set; }
    public String AddOper { get; set; }
    public DateTime ChgDate { get; set; }
    public String ChgOper { get; set; }

    [Required(ErrorMessage = "Birthdate is required")]
    [DataType(DataType.Date)]
    public DateTime Birthdate { get; set; }

    [StringLength(40)]
    public String NickName { get; set; }

    [StringLength(100)]
    [Required(ErrorMessage = "Address1 is required")]
    public String Address1 { get; set; }

    [StringLength(100)]
    public String Address2 { get; set; }

    [StringLength(100)]
    public String Address3 { get; set; }

    [StringLength(100)]
    [Required(ErrorMessage = "City is required")]
    public String City { get; set; }

    [Required(ErrorMessage = "Please select atleast one State.")]
    [StringLength(2)]
    public String State { get; set; }

    public String Zip { get; set; }

    [Required]
    [StringLength(200)]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
    [Display(Name = "Email address*")]
    public String EmailAddress { get; set; }

    [Required(ErrorMessage = "Please select atleast one Gender.")]
    public String Gender { get; set; }

    [Required(ErrorMessage = "Please select atleast one Marital Status.")]     
    public String MaritalStatus { get; set; }


    [StringLength(200)]
    public String SpouseName { get; set; }

    [DataType(DataType.Date)]
    public DateTime SpouseBirth { get; set; }

    public Int32 SpouseSSNO { get; set; }

    public Int32 EEOEthnic { get; set; }

    [Required(ErrorMessage = "Please select atleast one EEOJob.")]
    public Int32 EEOJob { get; set; }

    [Required(ErrorMessage = "Please select atleast one Citizen.")]  
    public Boolean Citizen { get; set; }

    public Decimal HomePhone { get; set; }

    [Range(0,int.MaxValue,ErrorMessage="Please enter proper MobilePhone.")]
    public Decimal MobilePhone { get; set; }

    public Decimal OfficePhone { get; set; }

    [StringLength(10)]
    public String OfficeExt { get; set; }

    [Range(0,int.MaxValue,ErrorMessage="BldgNo is too longer.")]
    [Required(ErrorMessage = "Building Number is required")]
    public Decimal BldgNo { get; set; }

    [StringLength(200)]
    [Required(ErrorMessage = "Emergency Contact is required")]
    public String EmergContact { get; set; }

    public Decimal EmergPhone { get; set; }

    [Required(ErrorMessage = "No Of Childeren is required")]
    public Int32 NoOfChilderen { get; set; }


}
1
  • Can you show Employee and EmployeeDetail Model Commented Dec 2, 2013 at 12:38

1 Answer 1

1

Try this as below

var list = (from g in db.Employee
   join d in db.db.EmployeeDetail on g.EGID equals d.EGID
   where g.EGID == id
   select new CombineModel
     {
          EE_GENERAL.PropertyName = g.ColumnName,
          EE_DEMOGRAPHIC.PropertyName = d.ColumnName
     });
Sign up to request clarification or add additional context in comments.

2 Comments

i tried it but i am unable to access property of EE_GENERAL or EE_DEMOGRAPHIC
Should that be `select new CombineModel { EE_GENERAL=g, EE_DEMOGRAPHIC=d }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.