c# - LINQ 实体结果到 List<string>

标签 c# silverlight wcf linq casting

我有一个查询,它只返回 silverlight4 域服务中实体的一列。如何将结果转换为列表?

public List<string> GetDataForTags() 
{
    var result =  from d in this.ObjectContext.vwBusinessUnits
                  select d.BusinessLineID.Distinct();
    return result;
}   

我尝试使用

return result as List<ToList();

return result.Cast<string>().ToList();

但是我从 Generic.IEnumerable<string> 得到一个无法隐式转换类型的错误至 Generic.List<string>

目前我可以使用

 var result =  from d in this.ObjectContext.vwBusinessUnits
                         select d.BusinessLineID;
            return result.Distinct().ToList();  

我正尝试在 View 模型中使用此结果,但出现转换错误

 private void LoadBUGroupTags()
    {
        TagsData = SecurityDomainContext.Current.GetDataForTags();
    }

Error 1 Cannot implicitly convert type 'System.ServiceModel.DomainServices.Client.InvokeOperation>' to 'System.Collections.Generic.List'

而 TagsData 只是一个公共(public)属性

public List<string> TagsData 
        {
            get 
            {
                return _tags;
            }
            set
            {
                if (_tags != value)
                {
                    _tags = value;
                 OnNotifyPropertyChanged("TagsData");
                }
            }
        }

最佳答案

不太清楚涉及的类型是什么 - 但 d.BusinessLineID是一个字符串,那么你将得到一个 IQueryable<IEnumerable<char>>这不是你想要的。

怀疑你想要:

var result =  from d in this.ObjectContext.vwBusinessUnits
              select d.BusinessLineID;
return result.Distinct().ToList();

没有查询表达式的写法更简单(IMO):

return this.ObjectContext.vwBusinessUnits
           .Select(d => d.BusinessLineID)
           .Distinct()
           .ToList();

关于c# - LINQ 实体结果到 List<string>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8857996/

相关文章:

c# - Azure Web 应用程序未在 Kudu 环境中显示连接字符串

c# - silverlight 列表框即使为空也会抛出 "value not fall in expected range"?

c# - 调整 Silverlight 数据网格行的单元格高度?

silverlight - 带有复选框和流向的 Silverlight 中的 Stage 效果

c# - WCF Streaming - 限制速度

c# - 关闭使用 WCF 的线程时的额外步骤?

c# - 如何在单个项目中使用 2 个不同的连接字符串?

java - c# 使用windows窗体调用jar文件

c# - 写入事件日志时如何避免此 SecurityException?

c# - WCF:如何在客户端缓存来自 OData 的集合