8,497
社区成员
发帖
与我相关
我的任务
分享
public class Student
{
public String ID { get; set; }
public String StuName { get; set; }
}
public class Grade
{
public String ID { get; set; }
public String GrideName { get; set; }
}
//测试数据
List<Student> tempStuList = new List<Student>() {
new Student(){ ID="001001", StuName="a"},
new Student(){ ID="002001", StuName="b"},
new Student(){ ID="002001", StuName="c"},
new Student(){ ID="003001", StuName="d"},
new Student(){ ID="001001", StuName="e"},
};
List<Grade> tempGradeList = new List<Grade>() {
new Grade(){ ID="001", GrideName = "g1"},
new Grade(){ ID="002", GrideName = "g2"},
new Grade(){ ID="003", GrideName = "g3"},
};
var a = from stu in tempStuList
join grade in tempGradeList
on stu.ID.Substring(0, 3) equals grade.ID
into tempGradeTable
from tempItem in tempGradeTable
select new { StuID = stu.ID, StuName = stu.StuName, GradeName = tempItem == null ? "" : tempItem.GrideName };
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public class Student
{
public String ID { get; set; }
public String StuName { get; set; }
}
public class Grade
{
public String ID { get; set; }
public String GrideName { get; set; }
}
static void Main(string[] args)
{
List<Student> tempStuList = new List<Student>() {
new Student(){ ID="001001", StuName="a"},
new Student(){ ID="002001", StuName="b"},
new Student(){ ID="002001", StuName="c"},
new Student(){ ID="003001", StuName="d"},
new Student(){ ID="003001", StuName="e"},
};
List<Grade> tempGradeList = new List<Grade>() {
new Grade(){ ID="001", GrideName = "g1"},
new Grade(){ ID="002", GrideName = "g2"},
new Grade(){ ID="003", GrideName = "g3"},
};
var a = from stu in tempStuList
select new { StuID = stu.ID, StuName = stu.StuName, GradeName = (tempGradeList.Where(x => stu.ID.IndexOf(x.ID) == 0).Count() > 0 ? tempGradeList.Where(x => stu.ID.IndexOf(x.ID) == 0).First().GrideName : "") };
a.ToList().ForEach(x => Console.WriteLine(x));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public class Student
{
public String ID { get; set; }
public String StuName { get; set; }
}
public class Grade
{
public String ID { get; set; }
public String GrideName { get; set; }
}
static void Main(string[] args)
{
List<Student> tempStuList = new List<Student>() {
new Student(){ ID="001001", StuName="a"},
new Student(){ ID="002001", StuName="b"},
new Student(){ ID="002001", StuName="c"},
new Student(){ ID="003001", StuName="d"},
new Student(){ ID="001001", StuName="e"},
};
List<Grade> tempGradeList = new List<Grade>() {
new Grade(){ ID="001", GrideName = "g1"},
new Grade(){ ID="002", GrideName = "g2"},
new Grade(){ ID="003", GrideName = "g3"},
};
var a = from stu in tempStuList
select new { StuID = stu.ID, StuName = stu.StuName, GradeName = (tempGradeList.Where(x => stu.ID.IndexOf(x.ID) == 0).SingleOrDefault().GrideName) };
a.ToList().ForEach(x => Console.WriteLine(x));
}
}
}
class GradeComparer : IEqualityComparer<string>
{
public bool Equals(string b1, string b2)
{
string stuId = b1.Length < b2.Length ? b2 : b1;
string gradeId = b1.Length < b2.Length ? b1 : b2;
return stuId.StartsWith(gradeId);
}
public int GetHashCode(string bx)
{
return 1; //返回同样的值,在Equals中进行真正的比较。可能效率会稍差。如果GetHashCode返回不同的值,Equals不会被调用。
}
}
GradeComparer gradeComparer = new GradeComparer();
var query = tempStuList.Join(tempGradeList,
s => s.ID,
g => g.ID,
(s, g) => new { ID = s.ID, Name = s.StuName, GID = g.ID, GName = g.GrideName},
gradeComparer);
foreach (var obj in query)
{
Console.WriteLine(obj.ID, obj.Name, obj.GID, obj.GName);
}
class GradeComparer : IEqualityComparer<string>
{
public bool Equals(string b1, string b2)
{
string stuId = b1.Length < b2.Length ? b2 : b1;
string gradeId = b1.Length < b2.Length ? b1 : b2;
return stuId.StartsWith(gradeId);
}
public int GetHashCode(string bx)
{
return bx.GetHashCode();
}
}
GradeComparer gradeComparer = new GradeComparer();
var query = tempStuList.Join(tempGradeList,
s => s.ID,
g => g.ID,
(s, g) => new { ID = s.ID, Name = s.StuName, GID = g.ID, GName = g.GrideName},
gradeComparer);
foreach (var obj in query)
{
Console.WriteLine(obj.ID, obj.Name, obj.GID, obj.GName);
}