开发者

Issue sorting array

I'm new to c#. I have an array values like this

id    name      subject    marks
1     jhone       math     60
2     smith       s开发者_开发技巧cience  50
3     clark       math     90
1     jhone       science  80
3     clark       science  56
1     jhone       ecnomics 75
2     smith       math     40
3     clark       ecnomics 99

first I want to sort array like this

id    name      subject    marks
1     jhone       math     60
1     jhone       science  80
1     jhone       ecnomics 75

2     smith       science  50
2     smith       math     40

3     clark       math     90
3     clark       science  56
3     clark       ecnomics 99

array.sort command working ok

now I want to concatenate these values and answer show like this

1,jhone,math 60,science 80,ecnomics 75
3,clark,math 90,science 56,ecnomics 99

Remember I'd like to not display content because id 2 have 2 subjects..his economics result is pending..so only show the above two lines in the result.

thanks in advance


I'm really not even sure what you're going for here, so if I'm off-base please provide a little more detail in to what you're trying to accomplish.

With that being said, using LINQ you can do this fairly easily:

First I start out with the object you're using. I've named it Foo, but this is (what appears to be) the structure you've supplied:

public class Foo
{
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public String Subject { get; set; }
    public Int32 Marks { get; set; }
}

Next, you can go through and group and sort by the ID, then pull the results that match that grouping:

var results = from b in Bar                     // Bar is my list of "Foo"s
              orderby b.Id ascending            // order it by ID
              group b by b.Id into grp       // then group them
              select new
              {
                  ID = grp.Key,                 // re-grab the ID
                  Foo = grp
              };

Now we go through the results and filter by the "has three options" you seem to be specifying. This is where you may need to be more diligent on how you determine a result is "outputable".

List<String> concat = new List<String>();
foreach (var result in results)
{
    if (result.Foo.Count() == 3) // has three options?
    {
        List<String> vals = new List<string>();
        vals.AddRange(new[]{ result.ID.ToString(), result.Foo.ElementAt(0).Name });

        foreach (var foo in result.Foo)
            vals.Add(String.Format("{0} {1}", foo.Subject, foo.Marks));
        concat.Add(String.Join(",", vals.ToArray()));
    }
}

And now concat has the items:

1,jhone,math 60,science 80,economics 75
3,clark,math 90,science 56,economics 99
Press any key to continue . . .


You should be able to get all the entries with certain values like this by using a Linq query. For instance,

StringBuilder sb = new StringBuilder();
foreach (String n in studentNames)
{
    IEnumerable<Record> query = from r in studentArray where r.Name == nameIter select r;
    sb.Append(r.id.ToString() + "," + r.name);
    foreach (Record r in query)
    {
        sb.Append("," + r.className);
        sb.Append(" " + r.score)
    }
    sb.Append("\n");
}

You had some other requirement to not show students with only 2 records but it wasn't very clear to me what you wanted.


Once you've sorted the array, go through it sequentially, building a List for each student. When the student number changes, only output if all subjects are covered.

Assuming you have this class:

class StudentGrade
{
    public int Id { get; private set; }
    public string Name { get; private set; }
    public string Subject { get; private set; }
    public int Marks { get; private set; }
    // Constructor and other things go here
}

And you have an array of those that you've sorted:

StudentGrade[] Grades;

Now, you want to output a line for each student that has all three grades:

List<StudentGrade> l = new List<StudentGrade>();
int LastId = -1;
foreach (var grade in Grades)
{
    if (grade.Id == LastId || LastId == -1)
    {
        // add this item to the list
        l.Add(grade);
    }
    else
    {
        // New student. Output data for previous student.
        if (l.Count == 3)
        {
            // output this student.
        }
        else
        {
            // student not output because not all grades are in.
        }
        LastId = grade.Id;
        l.Clear();
        l.Add(grade);
    }
}
// Check last student
if (l.Count == 3)
{
    // output last student
}
else
{
    // don't output because not all grades are in
}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜