现在的情况
目前我有这个Linq
查询:
return from function in this.context.Functions
join fp in this.context.FunctionParameters on function.FunctionID equals fp.FunctionID into functionParameters
from functionParameter in functionParameters.DefaultIfEmpty()
join d in this.context.Descriptions on new
{
ID = (int)function.FunctionID,
languageID = languageID,
originID = (byte)origin
}
equals new
{
ID = d.ValueID,
languageID = d.LanguageID,
originID = d.OriginID
}
into entityWithDescription
from x in entityWithDescription.DefaultIfEmpty()
select new FunctionDTO()
{
Function = function,
Description = x
};
这将返回函数及其参数和具体描述。所以,一个带有两个左外连接的选择。这一切都很好并且有效。
问题
我有多个具有描述的对象。描述表与这些对象没有关系(所以没有 FK)。所以上面的查询有一部分总是一样的,即对描述表的join查询:
join d in this.context.Descriptions on new
{
ID = (int)function.FunctionID,
languageID = languageID,
originID = (byte)origin
}
equals new
{
ID = d.ValueID,
languageID = d.LanguageID,
originID = d.OriginID
}
into entityWithDescription
变量languageID
和origin
是与方法一起传递的两个参数。FunctionID
是我Function
班级中的一个属性,即我的entity model
. 所以这是一个
public partial class Function
{
public byte FunctionID { get; set; }
/** Other properties **/
}
我的问题
linq
是否可以使用始终相同的查询部分创建一个单独的类?这样我就不必再次重复相同的代码了?
我已经尝试过的
var query = from function in this.context.Functions
join fp in this.context.FunctionParameters on function.FunctionID equals fp.FunctionID into functionParameters
from functionParameter in functionParameters.DefaultIfEmpty()
select function;
var testResult = this.context.Descriptions.GetDescriptionsByJoin(query, languageID, origin);
以及单独类中的重复代码:
public static IQueryable<IEnumerable<Description>> GetDescriptionsByJoin(
this IDbSet<Description> descriptions, IQueryable<ITranslatable> query, byte languageID, OriginEnum origin)
{
return from q in query
join d in descriptions on new
{
ID = q.ValueID,
languageID = languageID,
originID = (byte)origin
}
equals new
{
ID = d.ValueID,
languageID = d.LanguageID,
originID = d.OriginID
}
into entityWithDescription
select entityWithDescription;
}
但这给了我以下错误:
The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported
我知道我收到此错误是因为我在 join 语句中使用我的“valueID”作为参数,并且在我的实体模型中找不到该变量(valueID 是接口“ITranslatable”中的一个属性,我的所有类都有描述将实施)。
提前致谢!
问候 Loetn