我有这个方法:
private string GetLanguage(String currentLanguage, dynamic entity)
{
return (currentLanguage == "de" ? entity.language.German :
currentLanguage == "fr" ? entity.language.French :
currentLanguage == "en" ? entity.language.English :
entity.language.English);
}
我需要这个来返回一个像这个SQL语句那样的LINQ表达式:
select (case when [french] is null then [english] else [french] end) as language
from
我怎样才能做到这一点?
韩国社交协会
编辑:
更具体一点:
我有这样的事情:
myId = 1;
from objects in context.Objects
where objects.Id== myId
select new
{
MyObject = new Models.MyObjects()
{
Id = objects.ScrId,
Title = LanguageFactory.GetLabelLanguage(objects.Labels)
}
}).FirstOrDefault();
MyObject.Title必须使用某种语言(英语,法语,荷兰语等)。 在SQL中,我通过执行以下操作解决了这个问题:
select id, (case when [french] is null then [english] else [french] end) as language
from table
如何将LINQ表达式转换为类似上面的SQL查询?
答案 0 :(得分:2)
您将无法在Linq-to-SQL中调用任意函数。但您可以使用Expression
,如下所示:
private Expression<Func<DbObject, MyObject>> GetObjectWithLanguage =
e => new MyObject
{
Id = e.ScrId,
Title = e.currentLanguage == "de" ? e.Labels.language.German :
e.currentLanguage == "fr" ? e.Labels.language.French :
e.currentLanguage == "en" ? e.Labels.language.English :
e.Labels.language.English
};
...
var results = context.Objects.Where(e => e.Id == myId).Select(GetObjectWithLanguage);
这将在实体本身使用currentLanguage
属性来确定要返回的内容。但是,如果您想将currentLanguage
作为c#代码中的参数传递,请尝试以下操作:
private Expression<Func<DbObject, MyObject>> GetObjectWithLanguage(string currentLanguage)
{
if (currentLanguage == "de")
{
return e => new MyObject
{
Id = e.ScrId,
Title = e.Labels.language.German
}
}
else if (currentLanguage == "fr")
{
return e => new MyObject
{
Id = e.ScrId,
Title = e.Labels.language.French
}
}
...
}
var results = context.Objects.Where(e => e.Id == myId).Select(GetObjectsWithLanguage("de"));
如果您想创建一个通用方法,我建议您使用泛型而不是dynamic
参数,例如,假设您有一个类似的接口:
public interface ILanguageAware
{
string CurrentLanguage { get; }
string English { get; }
string French { get; }
string German { get; }
}
public class ObjectWithLanguage<T> where T : ILanguageAware
{
public T OriginalObject { get; set; }
string LanguageText { get; set; }
}
public Expression<Func<T, ObjectWithLanguage<T>>> GetObjectWithLanguage<T>() where T : ILanguageAware
{
return x => new ObjectWithLanguage<T>
{
OriginalObject = x,
LanguageText = x.CurrentLanguage == "de" ? x.German :
x.CurrentLanguage == "fr" ? x.French :
...
}
}
// extension method for convenience
public static IQueryable<ObjectWithLanguage<T>> SelectWithLanguage<T>(this IQueryable<T> queryable)
{
return queryable.Select(GetObjectWithLanguage<T>());
}
var results = context.Objectss.Where(e => e.Id == myId).SelectWithLanguage();