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/

相关文章:

silverlight - 显示 Silverlight 生成的消息

wcf - RabbitMQ Wcf 绑定(bind)

excel - 尝试使用 Microsoft.Office.Interop.Excel.Workbooks.Open() 打开 Excel 时出现异常 HResult 0x800a03ec

c# - 在c#中杀死ffmpeg中的进程

c# - CS0122 : 'System.Configuration.StringUtil' is inaccessible due to its protection level

c# - 是否可以为 Silverlight 中的静态资源提供类型转换器?

wcf - 获取 WCF 服务实现中参数的原始 XML

c# - Unity WebGL UnityWebRequest 坏体数据

c# - 基本的 CRC32 维基百科实现与在线看到的标准 CRC32 不同

silverlight - 如何查看 Silverlight dll 的源代码