Skip to content

Instantly share code, notes, and snippets.

@paul-cheung
Created March 13, 2014 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paul-cheung/9524891 to your computer and use it in GitHub Desktop.
Save paul-cheung/9524891 to your computer and use it in GitHub Desktop.
use a method and a certain property value to initialize another instance property in a class initialization.
public class ReflectionController : ODataController
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
[Queryable]
public IQueryable<CmsReflection> GetReflections()
{
var accessToken = Request.Headers.First(header => header.Key == "AccessToken").Value.First().ToString();
var db = Utility.GetDbContext(accessToken);
var reflections = db.Reflections.ToList();
var reflectionParticipants = db.ReflectionParticipants.ToList();
var competencies = db.CompetencyLangs.Where(cl => cl.LanguageId == (int)Language.Chinese).ToList();
var users = from user in db.DIR_Users
select new
{
Id = user.Id,
Name = user.Name
};
var cmsReflections = from r in reflections
select new CmsReflection
{
Id = r.Id,
CreatorUserId = r.CreatorUserId,
CreatedTime = r.CreatedTime,
CreatorRole = r.CreatorRole,
Type = r.Type,
CompetencyId = r.CompetencyId,
CompetencyName = (from c in competencies
where c.Id == r.CompetencyId
select c).FirstOrDefault().Name,
Subject = r.Subject,
Content = r.Content,
Status = r.Status,
ScoreFromCoach = r.ScoreFromCoach,
ParticipantsId = (from p in reflectionParticipants
where p.ReflectionId == r.Id
select p.UserId).ToList(),
CreatorUserName = (from u in users
where u.Id == r.CreatorUserId
select u.Name).FirstOrDefault(),
ParticipantsNameString = GetNamesString(ParticipantsId, users)
};
return new List<CmsReflection>().AsQueryable();
}
private string GetNamesString(List<Guid> ids, IEnumerable<dynamic> users)
{
var sb = new StringBuilder();
var filteredUserNames = users.Join(ids, user => user.Id, i => i, (user, i) => { return user.Name; }).ToList();
foreach (var userName in filteredUserNames)
{
sb.AppendLine(userName);
}
return sb.ToString();
}
public class CmsReflection
{
public Guid Id { get; set; }
public Guid CreatorUserId { get; set; }
public string CreatorUserName { get; set; }
public long CreatorRole { get; set; }
public long Type { get; set; }
public Guid CompetencyId { get; set; }
public string CompetencyName { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public long Status { get; set; }
public long ScoreFromCoach { get; set; }
public DateTime CreatedTime { get; set; }
public List<Guid> ParticipantsId { get; set; }
public string ParticipantsNameString { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment