我有这个实体:
public class Parent: AllDependant
{
    /*Properties goes here*/
}
public class Children: AllDependant
{
    /*Properties goes here*/
}
然后我有allDependantstypeof 的变量List<AllDependant>,它将保持一些父母和孩子的实体混合。
稍后,我想从中选择并执行以下操作:
var selectedDependantInfos = allDependants
        .Select(dependant =>
        {
            if (dependant is Parent)
            {
                var parent = dependant as Parent;
                return new { Name = parent.Name, SomeSpecialInfo = parent.ParentInfo };
            }
            else
            {
                var child = dependant as Children;
                return new { Name = child.Name, SomeSpecialInfo = child.ChildInfo }
            }
        });
请注意子级和父级的特定属性要求我将属性转换为用于 UI 显示的新模型,这与实体无关。我不能将特殊属性放在 AllDependant 基类中,因为我需要在包括 *.ascx 在内的许多文件上重构属性名称,这很麻烦。但是它是通过使用Select上面的 Linq 扩展方法完成的,但我只是想到了这一点:
问题:我怎样才能在 Linq Query 中做同样的事情?
这将在select关键字和花括号上给出错误:
var selectedDependantInfos = from dependant in allDependants
                            select
                            {
                                /* the same if statement goes here */
                            }