-1

我有这个方法:

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);
}

我需要它来返回一个 LINQ 表达式,它执行类似以下 SQL 语句的操作:

select (case when [french] is null then [english] else [french] end) as language 
from 

我怎样才能做到这一点?

Tks

编辑:

更具体:

我有这样的事情:

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 查询的东西?

4

1 回答 1

2

您将无法在 Linq-to-SQL 中调用任意函数。但是你可以使用 a 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();
于 2013-04-01T14:52:46.560 回答