在我的项目中,一个表与两个或多个表连接,要求想要输出需要加入它们,Join不是我的问题,在连接后想要从segregate表达式中选择所需的列,如同下面的语法:
public IEnumerable GetParent(string organogramType = "")
{
var query = (from p in this.Context.CmnCompanies
where organogramType != "" && !p.OrganogramType.Contains(organogramType) && p.OrganogramType != null
join r in this.Context.CmnCompanyCategories on p.CompanyCategoryID equals r.CompanyCategoryID
join s in this.Context.CmnCompanyReferences on p.RefID equals s.RefID
join t in this.Context.CmnPartnerDetails on p.PartnerID equals t.PartnerID).Select(SelectSearchColumns).ToList();
return query;
}
public Expression<Func<CmnCompany, CmnCompanyReference, CmnPartnerDetail, dynamic>> SelectSearchColumns = (p, r,t) => new
{
CompanyID = p.CompanyID,
CompanyName=p.CompanyName,
PartnerName=t.PartnerName,
OrganogramType=p.OrganogramType,
ParentID=p.ParentID,
InceptionDate=p.InceptionDate,
RefName=r.RefName,
};
想要从我的bello linq语法中隔离select语句
基于方法parameter=organogramType
选择列名称将会更改
假设organogramType (Company, office, departmentName) office
然后选择columnn “CompanyName “
将与Office混淆,如何在运行时chage列名
显示错误消息:查询正文必须以select子句或group子句结束
如果有任何疑问,请先谢谢
答案 0 :(得分:1)
你不能这样做,因为你的表达式需要三个参数,但是从查询中你总是会得到一个参数(它是序列类型)。此外,您的查询需要在 join 之后选择语句,以消除您现在看到的错误。因此,从技术上讲,您可以创建包含所有三个对象的类型:
public class Foo
{
public CmnCompany Company { get; set; }
public CmnCompanyReference CompanyReference { get; set; }
public CmnPartnerDetail PartnerDetail { get; set; }
}
从查询中选择该对象(不用担心,查询尚未执行):
var query =
from p in this.Context.CmnCompanies
where organogramType != "" &&
!p.OrganogramType.Contains(organogramType) &&
p.OrganogramType != null
join r in this.Context.CmnCompanyCategories
on p.CompanyCategoryID equals r.CompanyCategoryID
join s in this.Context.CmnCompanyReferences on p.RefID equals s.RefID
join t in this.Context.CmnPartnerDetails on p.PartnerID equals t.PartnerID)
select new Foo {
Company = p,
CompanyReference = s,
PartnerDetails = t
};
然后修改表达式(如果属性名称与原始属性名称匹配,则可以跳过属性名称指定):
public Expression<Func<Foo, dynamic>> SelectSearchColumns = (f) => new
{
f.Company.CompanyID,
f.Company.CompanyName,
f.PartnerDetail.PartnerName,
f.Company.OrganogramType,
f.Company.ParentID,
f.Company.InceptionDate,
f.CompanyReference.RefName,
};
并进行投影(仅在转换为列表时执行查询):
return query.Select(SelectSearchColumns).ToList();
如果您将运行SQL事件探查器,您将看到,只有表达式中选择的列才会从数据库返回。