2012-04-22 15 views
2

私は解決しようとしているLINQの問題を抱えています。私は複数のグループに属することができるユーザーがいるので、どのユーザーが単一のグループに属しているかを返すことができます。一般的な関連のlinqクエリ

List<Student> students = new List<Student>(); 
    public List<Student> ReturnStudentByGroupName(string groupName) 
    { 
     List<Student> student = (from g in students 
           where 
            (from t in g.StudentGroup where t.GroupName == groupName select t).Count() > 0 
           select g).ToList(); 
     return student; 
    } 

私の問題は今、私は複数のグループの共通のユーザーを見つける必要がありますか?たとえば、グループAとグループBの共通メンバーは誰ですか?この2つのグループのユーザーの一覧を検索していない場合、ユーザーはの場合のみ返信します。は両方のグループに属します。

2人の文字列を入力として使用する方法、つまりfirstgroupNameという文字列、secondgroupという文字列を知っている人は誰でも知っていますか?その後、一般の学生に戻しますか?グループの数に

答えて

1

さてあなたは、あなただけの両方のグループAとBに属しているユーザーのリストを返すようにしたいと述べましたですから、自然にwhereステートメントを1つではなく2つの条件で変更するだけで済みます。ここで

List<Student> students = new List<Student>(); 
    public List<Student> GetIntersectingStudents(string groupOne, string groupTwo) 
    { 
     var student = from s in students 
         let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName) 
         where grps.Contains(groupOne) && grps.Contains(groupTwo) 
         select s; 
     return student.ToList(); 
    } 
    public List<Student> GetIntersectingStudents(params string[] groups) 
    { 
     var student = from s in students 
         let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName) 
         where !groups.Except(grps).Any() 
         select s; 
     return student.ToList(); 
    } 

は法に二つの引数を取ります一つはあなたのためのカップルの方法、(あなたが求めて何を)とあなたが3から取得する必要がある場合(グループのリストを取り、その他、代わりの2つです、など)

EDIT:

私はちょうど楽しみのためだけでなく、そこにこの余分な方法を投げるだろうと思いました。すべてのグループとそのメンバーのリストをコンパイルします。

public static Dictionary<string, List<Student>> GetStudentGroups() 
    { 
     var temp = from s in students 
        let grps = s.StudentGroup.ConvertAll(gn => gn.GroupName) 
        from grp in grps 
        group s by grp 
        into g 
        select g; 
     return temp.ToDictionary(grping => grping.Key, studnt => studnt.ToList()); 
    } 
+0

真剣にそれを行いますか? –

+0

@JungleBoogie:うん、今はちょうどそれを修正しました。 – caesay

+0

ええええええと私は文字列に気づいただけでも、ちょうどそれを試して、事前に私の謝罪を受け入れるつもりです。 –

2
IEnumerable<Student> StudentsOfGroup(Group g) 
{ 
    return students.Where(s => s.StudentGroup.Contains(g)); 
} 

IEnumerable<Student> CommonStudents(IEnumerable<Group> groups) 
{ 
    return groups 
     .Select(StudentsOfGroup) 
     .Aggregate((acc, g) => acc.Intersect(g)); 
} 

または応じて以下がより速くなることができます:

IEnumberable<Student> CommonStudents(IEnumerable<Group> groups) 
{ 
    var groupSet = new HashSet<Group>(groups); 
    return students.Where(s => groupSet.IsSubsetOf(s.StudentGroup)); 
} 
+0

どのようにGETメソッドからクエリを実行しますか?鉱山に気付いた場合は、GETメソッドを使用してクエリを実行して結果を返すことができる "string groupName"があります。 –

+0

さて、 'string [] groups'を入力として受け入れるコードを簡単に修正することができます。 – Grozz

+2

グループタイプがあるので、それを使用する必要があります。グループに名前のみが含まれている場合(スニペットにすべて含まれている場合)、文字列のリストをGroupオブジェクトのリストに簡単に変換できます。しかし、その場合、Group Iの推測でEqualsメソッドを実装する必要があります。または、文字列を操作するコードを再作成してください。 –

2
IEnumberable<Student> GroupIntersection(IEnumerable<Group> groups) 
{ 
    return students 
     .Where(s => groups.All(g => s.StudentGroup.Contains(g))); 
} 
関連する問題