2

I am going to try and word this as simplified as possible because the question I have is rather complex ( or so I think!!!).

Currently I have a variable tied to a repeater, which lists a group of profiles for various employees within the business.

Within the variable, I have a field "Job_Title" which contains both the sector they work in and the job title separated by a '/'. What I am trying to achieve is assigning a set DIV Class based upon the sector within the "Job_Title" string.

Now I can achieve this on a single profile by doing the following

DT_Control_Profile Pro =
    db.DT_Control_Profiles
      .SingleOrDefault(x => x.PageControlID == PageControl_ID);

if (Pro != null)
{

    String[] cutsector = Pro.Job_Title.Split('/');
    foreach (string s in cutsector)
    {
        if (s.Trim().ToUpper() == "WELL ENGINEERING")
        {
            DIV_SECOTR.Attributes.Add("class", "sectorcon conwelleng");
        }
        else if (s.Trim().ToUpper() == "RESEVOIR ENGINEERING")
        {
            DIV_SECOTR.Attributes.Add("class", "sectorcon conreseng");
        }
        else if (s.Trim().ToUpper() == "GEO SCIENCES")
        {
            DIV_SECOTR.Attributes.Add("class", "sectorcon congeoscie");
        }
        else if (s.Trim().ToUpper() == "FACILITES ENGINEERING")
        {
            DIV_SECOTR.Attributes.Add("class", "sectorcon confacilieng");
        }
    };

However I am struggling to conjure a way of achieving this using a variable, which pulls multiple profiles onto a page!

So far I have this:

var leaders = from x in db.DT_Control_Profiles
              where x.FeatureProfile == true
                 && x.DT_PageControl.DT_SitePage.VennID == codesnippets.VennID
              select new
              {
                  img = Path + x.ImageUrl,
                  x.Job_Title,
                  x.Name,
                  about = codesnippets.StringSize(x.Biography, 100),
                  link = "~/" + x.DT_PageControl.DT_SitePage.PageName,
              };

I think that the solution would lie in a foreach loop but I have no idea where to begin!

Can anyone point me in the right direction??

2 Answers 2

1

You should be able to call a function inside your linq query. Create a function like this:

public static string GetHtmlClass(DT_Control_Profile Pro)
{
    String result = "";
    String[] cutsector = Pro.Job_Title.Split('/');
    foreach (string s in cutsector)
    {
        if (s.Trim().ToUpper() == "WELL ENGINEERING")
        {
            result += "sectorcon conwelleng ";
        }
        else if (s.Trim().ToUpper() == "RESEVOIR ENGINEERING")
        {
            result += "sectorcon conreseng ";
        }
        else if (s.Trim().ToUpper() == "GEO SCIENCES")
        {
            result += "sectorcon congeoscie ";
        }
        else if (s.Trim().ToUpper() == "FACILITES ENGINEERING")
        {
            result += "sectorcon confacilieng ";
        }
    }
}

After which your query should work like this:

var leaders = from x in db.DT_Control_Profiles
              where x.FeatureProfile == true
                 && x.DT_PageControl.DT_SitePage.VennID == codesnippets.VennID
              select new
              {
                  img = Path + x.ImageUrl,
                  x.Job_Title,
                  x.Name,
                  about = codesnippets.StringSize(x.Biography, 100),
                  link = "~/" + x.DT_PageControl.DT_SitePage.PageName,
                  class = GetHtmlClass(x)
              };

That should get you the class right in your query result.

Sign up to request clarification or add additional context in comments.

Comments

0

For starters I'd put your Sector -> Class mappings in a key value pair dictionary http://msdn.microsoft.com/en-us/library/xfhwa508.aspx It's just a bit cleaner and nicer way of doing the same thing.

I'm not sure where you're trying to store the class attribute, you could run through a foreach on the leaders collection and lookup from your dictionary and attach it, or you could have the lookup at the point of display.

Edit: As AVee says you could have the lookup within your linq statement, which if you only want the job title to display with the sector only selecting the div class I'd probably go for.

Comments

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.