Lambda Expression Type Conversion

  • Thread starter Thread starter Karanke
  • Start date Start date
K

Karanke

Guest
I'm trying to build a RESTful API and I've hit a snag. I'm getting type Conversion Error in Lambda Expression. Here's my Code


using System;

namespace DataAccess.BO
{
public class User
{
public int Id { get; set; }
public int? BossId { get; set; }
public string Title { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public DateTime DateOfBirth { get; set; }
public Gender Gender { get; set; }

}
}




PersonDataAccess.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataAccess.BO;

namespace DataAccess
{
public class PersonDataAccess
{
#region Data
public static readonly List<User> Data = new List<User>
{
new User
{
Id = 8,
GivenName = "Trinh",
FamilyName = "Montejano",
BossId = 3,
Title = "Tech Manager",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1966-09-27")
},
new User
{
Id = 1,
GivenName = "Winfred",
FamilyName = "Fetzer",
BossId = null,
Title = "CEO",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1927-01-29")
},
new User
{
Id = 2,
GivenName = "Erich",
FamilyName = "Dandrea",
BossId = 1,
Title = "VP of Marketing",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1927-08-20")
},
new User
{
Id = 3,
GivenName = "Reinaldo",
FamilyName = "Nisbet",
BossId = 1,
Title = "VP of Technology",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1929-02-07")
},
new User
{
Id = 4,
GivenName = "Alleen",
FamilyName = "Bufford",
BossId = 1,
Title = "VP of HR",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1932-06-13")
},
new User
{
Id = 5,
GivenName = "Kristyn",
FamilyName = "Klopfer",
BossId = 2,
Title = "Director of Marketing",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1936-09-26")
},
new User
{
Id = 6,
GivenName = "Sophie",
FamilyName = "Duhon",
BossId = 3,
Title = "Tech Manager",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1937-11-23")
},
new User
{
Id = 7,
GivenName = "Suanne",
FamilyName = "Mirabal",
BossId = 3,
Title = "Tech Manager",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1948-04-05")
},
new User
{
Id = 9,
GivenName = "Norah",
FamilyName = "Maslowski",
BossId = 4,
Title = "Tech Manager",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1966-10-13")
},
new User
{
Id = 10,
GivenName = "Gertrudis",
FamilyName = "Redford",
BossId = 6,
Title = "Tech Lead",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1967-08-25")
},
new User
{
Id = 11,
GivenName = "Donovan",
FamilyName = "Tobey",
BossId = 6,
Title = "Tech Lead",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1968-12-26")
},
new User
{
Id = 12,
GivenName = "Rich",
FamilyName = "Vermeulen",
BossId = 9,
Title = "Trainer Lead",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1969-10-16")
},
new User
{
Id = 13,
GivenName = "Santo",
FamilyName = "Knupp",
BossId = 9,
Title = "HR Manager",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1972-10-16")
},
new User
{
Id = 14,
GivenName = "Jazmin",
FamilyName = "Grooms",
BossId = 12,
Title = "Trainer",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1974-03-23")
},
new User
{
Id = 15,
GivenName = "Annelle",
FamilyName = "Cheeks",
BossId = 13,
Title = "Recruiter",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1978-08-25")
},
new User
{
Id = 16,
GivenName = "Eliza",
FamilyName = "Harshaw",
BossId = 12,
Title = "Trainer",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1979-08-21")
},
new User
{
Id = 17,
GivenName = "Xiomara",
FamilyName = "Broaddus",
BossId = 8 ,
Title = "Senior Software Developer",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1980-02-09")
},
new User
{
Id = 18,
GivenName = "Erminia",
FamilyName = "Jungers",
BossId = 11,
Title = "Software Developer",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1981-09-08")
},
new User
{
Id = 19,
GivenName = "Maria",
FamilyName = "Moffatt",
BossId = 10,
Title = "Software Developer",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1984-03-18")
},
new User
{
Id = 20,
GivenName = "Tammera",
FamilyName = "Grimaldo",
BossId = 10,
Title = "Senior Software Developer",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1990-09-24")
},
new User
{
Id = 21,
GivenName = "Sharyl",
FamilyName = "Das",
BossId = 10,
Title = "Software Developer",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1992-06-18")
},
new User
{
Id = 22,
GivenName = "Shan",
FamilyName = "Harlan",
BossId = 8,
Title = "UI Developer",
Gender = Gender.Unspecified,
DateOfBirth = DateTime.Parse("1993-11-15")
},
new User
{
Id = 23,
GivenName = "Mariah",
FamilyName = "Almeida",
BossId = 11,
Title = "QA Tester",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("1997-03-23")
},
new User
{
Id = 24,
GivenName = "Darnell",
FamilyName = "Kerfien",
BossId = 11,
Title = "QA Tester",
Gender = Gender.Male,
DateOfBirth = DateTime.Parse("1998-11-10")
},
new User
{
Id = 25,
GivenName = "Janell",
FamilyName = "Vierra",
BossId = 11,
Title = "QA Tester",
Gender = Gender.Female,
DateOfBirth = DateTime.Parse("2004-04-22")
}
};
#endregion
}
}



and My Controller


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DataAccess;


namespace Sr.Controllers
{
public class UserController : ApiController
{

/// <summary>
/// Get Person By ID
/// </summary>
/// <param name="id">Person id </param>
/// <returns></returns>
// GET: api/User/GetByID/5
[Route("api/User/GetByID/{id}")]
public IHttpActionResult GetById(int id)
{
PersonDataAccess person = PersonDataAccess.Data.FirstOrDefault(p => p.Id == id);
if (person != null)
{
return Ok(person);
}
else
{
return NotFound();
}
}
}
}


I'm getting an error "Cannot implicitly convert type 'Data.BO.<g class="gr_ gr_234 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="234" id="234">User</g>' to 'DataAccess.PersonDataAccess'" in this line

PersonDataAccess person = PersonDataAccess.Data.FirstOrDefault(p => p.Id == id);



What am I doing wrong here and how can I fix it?

Continue reading...
 
Back
Top