两结构组合新结构linq级联查询例子

3 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            List<a> aa = new List<a>();
            List<b> bb = new List<b>();
            List<c> cc = new List<c>();

            aa.Add(new a(1));
            aa.Add(new a(2));
            aa.Add(new a(3));
            aa.Add(new a(4));
            aa.Add(new a(5));
            bb.Add(new b(1, 93));
            bb.Add(new b(2, 83));
            bb.Add(new b(3, 63));
            cc.Add(new c(2, 63));
            cc.Add(new c(3, 43));
            cc.Add(new c(4, 33));
            //将 a,b,c 使用 ID 级联 组合
            var query = from aaa in aa
                        join bbb in bb on aaa.ID equals bbb.ID into bbb
                        from bbb2 in bbb.DefaultIfEmpty()
                        join ccc in cc on aaa.ID equals ccc.ID into ccc
                        from ccc2 in ccc.DefaultIfEmpty()
                        select new a(aaa.ID)
                        {
                            i= bbb2== null ? null: bbb2.i,
                            j= ccc2== null ? null : ccc2.j
                        };
            aa= query.ToList<a>();
        }
    }
    public class a
    {
        public a()
        {
            i = -1;
            j = -1;
        }
        public a(int id){
            ID = id;
            i = 0;
            j = 0;
            }
        public int ID;
        public int? i;
        public int? j;
    }
    public class b
    {
        public b(int id,int val)
        {
            ID = id;
            i=val;
        }
        public int ID;
        public int? i;
    }

    public class c
    {
        public c(int id, int val)
        {
            ID = id;
            j = val;
        }
        public int ID;
        public int? j;
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值