问题描述
无法从用法中推导出方法“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());
}