适合个人站长的网站有哪些,友妙招链接怎么弄,旅游商城网站模板,电子商城网站开发流程在C#中#xff0c;IEnumerable 的 Join 方法用于根据键将两个序列中的元素进行关联。Join 方法通常用于执行类似于 SQL 中的内连接操作。以下是 Join 方法的基本用法#xff1a;
基本语法
public static IEnumerableTResult JoinTOuter, TInner, TKey, TResult…
在C#中IEnumerable 的 Join 方法用于根据键将两个序列中的元素进行关联。Join 方法通常用于执行类似于 SQL 中的内连接操作。以下是 Join 方法的基本用法
基本语法
public static IEnumerableTResult JoinTOuter, TInner, TKey, TResult(this IEnumerableTOuter outer,IEnumerableTInner inner,FuncTOuter, TKey outerKeySelector,FuncTInner, TKey innerKeySelector,FuncTOuter, TInner, TResult resultSelector
)参数说明
• outer: 外部数据源。
• inner: 内部数据源。
• outerKeySelector: 一个函数用于从外部数据源的每个元素提取键。
• innerKeySelector: 一个函数用于从内部数据源的每个元素提取键。
• resultSelector: 一个函数用于定义结果投影。
示例
假设我们有两个集合一个是学生集合另一个是成绩集合我们希望根据学生的ID将这两个集合关联起来。
定义测试用的数据类 /// summary/// 学生信息/// /summarypublic class Student{public int Id { get; set; }public string Name { get; set; }}/// summary/// 学生成绩/// /summarypublic class Grade{public int StudentId { get; set; }/// summary/// 科目/// /summarypublic string Subject { get; set; }/// summary/// 成绩/// /summarypublic int Score { get; set; }}创建数据 var students new ListStudent{new Student { Id 1, Name 张三 },new Student { Id 2, Name 李四 },new Student { Id 3, Name 王五 },};var grades new ListGrade{new Grade{StudentId 1,Subject 数学,Score 90,},new Grade{StudentId 2,Subject 科学,Score 85,},new Grade{StudentId 1,Subject 历史,Score 88,},};使用 Join 方法
在存储数据时都是通过ID来确定唯一性不会重复存储Name之类的而在使用的时候显示在界面上用户不知道ID的意义则需要显示Name。
在代码中就需要处理使用Join方法返回学生跟成绩关联信息可以知道谁什么课考了多少分。
var studentGrades students.Join(grades,student student.Id,grade grade.StudentId,(student, grade) new{StudentName student.Name,GradeSubject grade.Subject,GradeScore grade.Score});foreach (var sg in studentGrades)
{Console.WriteLine($Student: {sg.StudentName}, Subject: {sg.GradeSubject}, Score: {sg.GradeScore});
}运行测试 在这个示例中我们使用 Join 方法将 students 和 grades 集合根据学生的ID进行了关联并创建了一个匿名对象来存储结果。你可以根据需要调整 resultSelector 来定义不同的结果结构。
官方文档
https://learn.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable.join?viewnet-9.0redirectedfromMSDN#overloads 文章转载自: http://www.morning.yrfxb.cn.gov.cn.yrfxb.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.cyjjp.cn.gov.cn.cyjjp.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.ltqzq.cn.gov.cn.ltqzq.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.tlrxt.cn.gov.cn.tlrxt.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.rfjmy.cn.gov.cn.rfjmy.cn http://www.morning.mcndn.cn.gov.cn.mcndn.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.skmpj.cn.gov.cn.skmpj.cn http://www.morning.qinhuangdjy.cn.gov.cn.qinhuangdjy.cn http://www.morning.mzcsp.cn.gov.cn.mzcsp.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.wsgyq.cn.gov.cn.wsgyq.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.dsxgc.cn.gov.cn.dsxgc.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.bmgdl.cn.gov.cn.bmgdl.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.qcymf.cn.gov.cn.qcymf.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.rzbgn.cn.gov.cn.rzbgn.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.kcfnp.cn.gov.cn.kcfnp.cn http://www.morning.rcfwr.cn.gov.cn.rcfwr.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.kqglp.cn.gov.cn.kqglp.cn