【语言-c#】Linq 使用Join关联表时,A表和B表关联的字段类型不一样

 问题描述

无法从用法中推导出方法“System.Linq.Enumerable.Join<TOuter,TInner,TKey,TResult>(System.Collections.Generic.IEnumerable<TOuter>, System.Collections.Generic.IEnumerable<TInner>, System.Func<TOuter,TKey>, System.Func<TInner,TKey>, System.Func<TOuter,TInner,TResult>)”的类型实参。请尝试显式指定类型实参。	

分析

①、使用 equals 、 =、== 、比较的类型不一样;

②、使用 equals 、 =、== 、比较的字段名或别名不一样;

解决方案

统一equals 、 =、== 、比较的类型以及别名。

①、使用单边的强制转换;

②、双边统一转换成字符串;

③、统一使用 new 来修改字段别名。

LINQ 语法

using (Models.atdpsEntities db = new Models.atdpsEntities())
{
    var query = from a in db.prescription
                join b in db.patient 
                on new { pid = a.patient_id.ToString() }
                equals new { pid = b.patient_id.ToString() }
                select a;
    System.Windows.Forms.MessageBox.Show(query.ToList().Count.ToString());
}

Lambda 式

using (Models.atdpsEntities db = new Models.atdpsEntities())
{
    var query = db.prescription.Join(db.patient, 
        a => a.patient_id.ToString(), 
        b => b.patient_id.ToString(), 
        (a, b) => new { section1 = a.patient_id, section2 = b.patient_hint });
    System.Windows.Forms.MessageBox.Show(query.ToList().Count.ToString());
}

 

using (Models.atdpsEntities db = new Models.atdpsEntities())
{
    var query = db.prescription.Join(db.patient,
        a => new { pid = a.patient_id.ToString() },
        b => new { pid = b.patient_id.ToString() },
        (a, b) => new { section1 = a.patient_id, section2 = b.patient_hint });
    System.Windows.Forms.MessageBox.Show(query.ToList().Count.ToString());
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值