1

目前我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;
    }
}

谁能帮我解决这个问题?

4

6 回答 6

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();
于 2012-07-30T10:51:11.990 回答
3

您在此处使用投影方法创建匿名类型:Select

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

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

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

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

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

于 2012-07-30T10:50:35.413 回答
2

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

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

如果您只想要 SubGroupName:

(subGroup, group) => subGroup.cSubGroupName
于 2012-07-30T10:50:56.843 回答
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();
于 2012-07-30T10:51:05.530 回答
1

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

于 2012-07-30T10:53:14.610 回答
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; 
            }
        }
于 2012-07-30T10:59:10.023 回答