这个lambda表达式的返回类型应该是什么?

时间:2012-07-30 10:46:36

标签: c# lambda

目前我在两个表上执行join,结果可能是多行。

目前返回类型为IList<string>但我收到错误

错误:

Cannot implicitly convert type System.Collections.Generic.List<AnonymousType#1> to System.Collections.Generic.IList<string>

这是我的表达

public IList<string> GetPendingSubGroups()
{
    using (var db = new DataClasses1DataContext())
    {
        var pendingSubGroup =
            db.sys_Log_Account_SubGroups.Where(subGroup => subGroup.cAuthorizedStatus.Equals("Pending")).Join(
                db.sys_Account_Primary_Groups, subGroup => subGroup.nGroupCode, group => group.nGroupCode,
                (subGroup, group) => new
                                         {
                                             cSubGroupName = subGroup.cSubGroupName,
                                             cAddedBy = subGroup.cAddedBy,
                                             dAddedOn = subGroup.dAddedOn
                                         }).ToList();
        return pendingSubGroup;
    }
}

任何人都可以帮我解决这个问题吗?

6 个答案:

答案 0 :(得分:4)

创建一个表示结果的类

public class SubGroup
{
    public string Name {get; set;}
    public string AddedBy {get; set;}        
    public DateTime AddedOn {get; set;}
}

并返回该类的列表

public IList<SubGroup> GetPendingSubGroups()
    {
        using(var db=new DataClasses1DataContext())
        {
            var pendingSubGroup = db.sys_Log_Account_SubGroups.Where(subGroup => subGroup.cAuthorizedStatus.Equals("Pending")).Join(db.sys_Account_Primary_Groups, subGroup => subGroup.nGroupCode, group => group.nGroupCode,(subGroup, group) => new SubGroup
            {
                Name = subGroup.cSubGroupName,
                AddedBy = subGroup.cAddedBy,
                AddedOn = subGroup.dAddedOn 
            }).ToList();  
            return pendingSubGroup; 
        }
    }

注意这个变化埋没在linq

(subGroup, group) => new *SubGroup*
            {
                Name = subGroup.cSubGroupName,
                AddedBy = subGroup.cAddedBy,
                AddedOn = subGroup.dAddedOn 
            }).ToList();

答案 1 :(得分:3)

您正在使用Select投影方法创建Anonymous Type

new
{
    cSubGroupName = subGroup.cSubGroupName,
    cAddedBy = subGroup.cAddedBy,
    dAddedOn = subGroup.dAddedOn 
}

类型由编译器创建(作为具有只读属性的类)。此实例中的属性名称为cSubGroupNamecAddedBydAddedOn

如果您需要返回string,或许您需要返回其中一个属性而不是整个类型?

您将无法在匿名类型和字符串之间实现转换,因此您可能需要首先不创建类型,只需选择字符串属性。

如果您需要返回一个类型而不仅仅是string,那么只需创建一个具有此属性的自定义类,而不是创建一个匿名类型,创建一个类的实例,如{{3} }。

答案 2 :(得分:2)

错误消息几乎说明了这一点,您将返回一个匿名类型列表:

(subGroup, group) => new
{
    cSubGroupName = subGroup.cSubGroupName,
    cAddedBy = subGroup.cAddedBy,
    dAddedOn = subGroup.dAddedOn 
}

如果您只想要SubGroupName:

(subGroup, group) => subGroup.cSubGroupName

答案 3 :(得分:2)

返回类型是匿名类型。您应该创建一个包含这些属性的新类,并按如下方式使用它:

public IList<SomeClass> GetPendingSubGroups()
    {
        using(var db=new DataClasses1DataContext())
        {
            var pendingSubGroup = db.sys_Log_Account_SubGroups
            .Where(subGroup => subGroup.cAuthorizedStatus.Equals("Pending"))
            .Join(db.sys_Account_Primary_Groups, subGroup => subGroup.nGroupCode, group => group.nGroupCode,(subGroup, group) => new SomeClass(subGroup.cSubGroupName, subGroup.cAddedBy, subGroup.dAddedOn)).ToList();  

            return pendingSubGroup; 
        }
    }

但似乎你只想要返回一个字符串?在这种情况下,您可以只投影一个字段:

return pendingSubGroup.Select(p => p.cSubGroupName).ToList();

答案 4 :(得分:1)

您可以退回IList。但是每次尝试使用它时都必须打开它。我不打算这样做。最好在返回时定义自己的类

答案 5 :(得分:1)

如前所述,您创建一个由三个属性组成的匿名类型:

cSubGroupName 
cAddedBy 
dAddedOn 

如果您想获得 SubGroupName ,那么您可以修改您的qu

public IList<string> GetPendingSubGroups()
        {
            using(var db=new DataClasses1DataContext())
            {
                var pendingSubGroup = db.sys_Log_Account_SubGroups.Where(subGroup => subGroup.cAuthorizedStatus.Equals("Pending")).Join(db.sys_Account_Primary_Groups, subGroup => subGroup.nGroupCode, group => group.nGroupCode,(subGroup, group) => subGroup.cSubGroupName).ToList();  
                return pendingSubGroup; 
            }
        }