如何在对象声明中添加条件? EF -

时间:2010-12-01 10:42:48

标签: c# entity-framework linq-to-entities

我想做这样的事情(如下)......我不想完全写出template.title = xxx,template.descrption = xxx等。

但我无法编译,因为条件不正确。我似乎可以添加像这样的条件 (郎==“E”)? doctype =“Spot-II:”:声明中的doctype =“Spot-IIII”

等等......

有人知道如何获得(lang ==“E”)条件在下面工作吗?

        foreach (var item in s)
        {
            template = new RSSTemplate()
            {
                title = item.titre,
                description = item.description,
                (lang == "E") ? doctype = "Spot-II: " : doctype = "Spot-IIII "
            };
            t.Add(template);
        }

1 个答案:

答案 0 :(得分:2)

也许这会奏效:

  foreach (var item in s) 
    { 
        template = new RSSTemplate() 
        { 
            title = item.titre, 
            description = item.description, 
            doctype = (lang == "E") ? "Spot-II: " : "Spot-IIII " 
        }; 
        t.Add(template); 
    } 
相关问题