LINQ 查询实体类的类型不符

jcyluck 2016-09-03 11:55:02
PART_Collection<PART_List> l_list = g_Value["AA" ];
PART_Collection<PART_List> l_templist = l_list.Where(p => p.time <= g_readtime).ToList();

这两行代码的结果却是报错的:
无法将类型“System.Collections.Generic.List<Test.PART_List>”
隐式转换为 “Test.PART_Collection<Test.PART_List>"

为什么说集合的类型不一致呢?从 PART_Collection<PART_List> 中查询出来的集合类型怎么就不是 PART_Collection<PART_List> 了?有什么方法解决这个问题吗?
...全文
211 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jcyluck 2016-09-03
  • 打赏
  • 举报
回复
引用 1 楼 qq_17486399 的回复:
加一个强制转换。
你tolist后类型不再是你原来的类型了




强制转换后,编译可以通过,但运行时还是提示类型不对。
jcyluck 2016-09-03
  • 打赏
  • 举报
回复
引用 3 楼 sp1234 的回复:
[quote=引用 楼主 jcyluck 的回复:]
这两行代码的结果却是报错的:
无法将类型“System.Collections.Generic.List<Test.PART_List>”
隐式转换为 “Test.PART_Collection<Test.PART_List>"

为什么说集合的类型不一致呢?从 PART_Collection<PART_List> 中查询出来的集合类型怎么就不是 PART_Collection<PART_List> 了?有什么方法解决这个问题吗?


人家明明说的是“List<PART_List> 不是 PART_Collection<PART_List>”,你可不能这么明显地歪曲提示信息啊。[/quote]



上图是原始代码及提示,加TOLIST()是我打错了。
下面是代码部分:

                                       
RSI_Collection<Rsi_List> l_list = g_Value_RSI["AA"];
var l_var = l_list.Where(p => p.time <= g_readtime).ToList();

RSI_Collection<Rsi_List> l_templist1 = l_list.Where(p => p.time <= g_readtime);
  • 打赏
  • 举报
回复
引用 楼主 jcyluck 的回复:
这两行代码的结果却是报错的: 无法将类型“System.Collections.Generic.List<Test.PART_List>” 隐式转换为 “Test.PART_Collection<Test.PART_List>" 为什么说集合的类型不一致呢?从 PART_Collection<PART_List> 中查询出来的集合类型怎么就不是 PART_Collection<PART_List> 了?有什么方法解决这个问题吗?
人家明明说的是“List<PART_List> 不是 PART_Collection<PART_List>”,你可不能这么明显地歪曲提示信息啊。
  • 打赏
  • 举报
回复
PART_Collection 是什么东西,谁能看出来?
大鱼> 2016-09-03
  • 打赏
  • 举报
回复
加一个强制转换。 你tolist后类型不再是你原来的类型了
jcyluck 2016-09-03
  • 打赏
  • 举报
回复
引用 13 楼 xuzuning 的回复:
看看 C# 把他识别成什么类型
            //TEST_Collection<TEST_LIST>
            var l_temp = l_list.Where(p => p.id > 5);
            Console.WriteLine(l_temp.GetType());
            foreach (var x in l_temp) Console.WriteLine("id:{0} name:{1}", x.id, x.name);
是的,我也看到了,但怎么解决这个问题呢?谢谢!
xuzuning 2016-09-03
  • 打赏
  • 举报
回复
看看 C# 把他识别成什么类型
            //TEST_Collection<TEST_LIST>
var l_temp = l_list.Where(p => p.id > 5);
Console.WriteLine(l_temp.GetType());
foreach (var x in l_temp) Console.WriteLine("id:{0} name:{1}", x.id, x.name);
jcyluck 2016-09-03
  • 打赏
  • 举报
回复
引用 6 楼 qq_17486399 的回复:
[quote=引用 5 楼 jcyluck 的回复:] [quote=引用 1 楼 qq_17486399 的回复:] 加一个强制转换。 你tolist后类型不再是你原来的类型了
强制转换后,编译可以通过,但运行时还是提示类型不对。[/quote] PART_Collection<PART_List> l_templist = l_list.Where(p => p.time <= g_readtime).ToList() as PART_Collection<PART_List> ; 你试试这样呢?[/quote] 一样的。
jcyluck 2016-09-03
  • 打赏
  • 举报
回复
附错误提示图
jcyluck 2016-09-03
  • 打赏
  • 举报
回复
引用 9 楼 sp1234 的回复:
Linq 查询结果跟你的自定义的那个类型,凭什么就兼容呢?你没有给出任何说明。

不要胡乱“强制类型转换”。关键不在于类型转换,关键在于这种想当然的习惯来误导你自己,最终导致你不求甚解的后果。



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Trade_Test
{

public class TEST_LIST
{
public int id { get; set; }
public string name { get; set; }
}

public class TEST_Collection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
private int a_MaxCount = 0;
/// <summary>
/// 数组的最大值,超过这个值,就会删除第一条记录
/// </summary>
public int s_MaxCount
{
get { return a_MaxCount; }
set { a_MaxCount = value; }
}

public TEST_Collection()
{
base.CollectionChanged += TEST_Collection_CollectionChanged;
}

private void TEST_Collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
if (Count > a_MaxCount) { RemoveAt(0); }
}
}
}

static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{

TEST_Collection<TEST_LIST> l_list = new TEST_Collection<TEST_LIST>();
l_list.s_MaxCount = 5;
l_list.Add(new TEST_LIST { id = 1, name = "11" });
l_list.Add(new TEST_LIST { id = 2, name = "22" });
l_list.Add(new TEST_LIST { id = 3, name = "33" });
l_list.Add(new TEST_LIST { id = 4, name = "44" });
l_list.Add(new TEST_LIST { id = 5, name = "55" });
l_list.Add(new TEST_LIST { id = 6, name = "66" });
l_list.Add(new TEST_LIST { id = 7, name = "77" });

TEST_Collection<TEST_LIST> l_temp = l_list.Where(p => p.id > 5);

}
}
}



错误 CS0266 无法将类型“System.Collections.Generic.IEnumerable<Trade_Test.TEST_LIST>”隐式转换为“Trade_Test.TEST_Collection<Trade_Test.TEST_LIST>”。存在一个显式转换(是否缺少强制转换?)

谢谢大神关注,这是全部的代码,大神 再看看,我想说的是, l_list.Where 查询出来的数据类型为什么就不是 TEST_Collection<TEST_LIST> 了,这个要怎么解决?
  • 打赏
  • 举报
回复
Linq 查询结果跟你的自定义的那个类型,凭什么就兼容呢?你没有给出任何说明。 不要胡乱“强制类型转换”。关键不在于类型转换,关键在于这种想当然的习惯来误导你自己,最终导致你不求甚解的后果。
  • 打赏
  • 举报
回复
引用 5 楼 jcyluck 的回复:
强制转换后,编译可以通过,但运行时还是提示类型不对。
不要自欺欺人地去“强制类型转换”,你要能说出点稍微深度来。如果你不知道
a = 1;
为什么出错(例如a可能声明是一个字典类型的),那么就回到刚学的时候的状态。 而你这个帖子,根本不知道你所谓的 PART_Collection<> 是什么东东,难道就靠胡乱瞎猜就能编程?
  • 打赏
  • 举报
回复
引用 4 楼 jcyluck 的回复:
上图是原始代码及提示,加TOLIST()是我打错了。 下面是代码部分:
                                       
 RSI_Collection<Rsi_List> l_list = g_Value_RSI["AA"];
 var l_var = l_list.Where(p => p.time <= g_readtime).ToList();

  RSI_Collection<Rsi_List> l_templist1 = l_list.Where(p => p.time <= g_readtime);
不管是 List<> 还是 IEnumerable<>,总之不是 RSI_Collection<>。 你的这个类型能根据什么来使用枚举来赋值呢?你并没有半点说明。
大鱼> 2016-09-03
  • 打赏
  • 举报
回复
引用 5 楼 jcyluck 的回复:
[quote=引用 1 楼 qq_17486399 的回复:] 加一个强制转换。 你tolist后类型不再是你原来的类型了
强制转换后,编译可以通过,但运行时还是提示类型不对。[/quote] PART_Collection<PART_List> l_templist = l_list.Where(p => p.time <= g_readtime).ToList() as PART_Collection<PART_List> ; 你试试这样呢?
LINQ is the part of the .NET Framework that provides a generic approach to querying data from different data sources. It has quickly become the next must-have skill for .NET developers. Pro LINQ: Language Integrated Query in C# 2010 is all about code. Literally, this book starts with code and ends with code. Most books show the simplest examples of how to use a method, but they so rarely show how to use the more complex prototypes. This book is different. Demonstrating the overwhelming majority of LINQ operators and prototypes, it is a veritable treasury of LINQ examples. Rather than obscure the relevant LINQ principles in code examples by focusing on a demonstration application you have no interest in writing, this book cuts right to the chase of each LINQ operator, method, or class. However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it. For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts so you can step through the code and see them unfold. Face it, most technical books, while informative, are dull. LINQ need not be dull. Written with a sense of humor, this book will attempt to entertain you on your journey through the wonderland of LINQ and C# 2010. What you’ll learn How to query Databases with LINQ to SQL, write your own entity classes, and understand how to handle concurrency conflicts. * How to leverage all the new LINQ relevant C# 2008 language features including extension methods, lambda expressions, anonymous data types, and partial methods. * How to use LINQ to Objects to query in-memory data collections such as arrays, ArrayLists, and Lists to retrieve the data you want. * Why some queries are deferred, how a deferred query can bite you, and how you can make deferred queries work for you. * How to use LINQ to XML to revolutionize your creation, manipulation, and searching of XML data. * How to query DataSets with LINQ to DataSet so you can coexist with legacy code and use LINQ to query databases other than SQL Server. Who is this book for? Apress, 2010 This book is written for the proficient C# developer, but you do not need to be up on all the latest C# features to understand the material. When you finish this book, you will have a much greater understanding of the latest C# features. amazon link:http://www.amazon.com/exec/obidos/ASIN/1430226536/buythisbooks-20
.NET Framework 4.0 常用类库参考手册 [微软官方 MSDN] MSDN Library - .NET Framework 4.0 - 命名空间 (节选版) 制作成了chm格式,方便离线状态下学习! ======================================= 原文地址:http://msdn.microsoft.com/zh-cn/library/ms229335.aspx ======================================= .NET Framework 类库 - 命名空间 (节选版) Microsoft.Win32 提供两种类型的类:处理由操作系统引发的事件的类和对系统注册表进行操作的类。 System 包含用于定义常用值和引用数据类型、事件和事件处理程序、接口、特性和处理异常的基础类和基类。其他类提供支持下列操作的服务:数据类型转换,方法参数操作,数学计算,远程和本地程序调用,应用程序环境管理以及对托管和非托管应用程序的监管。 System.Collections 包含定义各种对象集合(如列表、队列、位数组、哈希表和字典)的接口和类。 System.Data 包含组成大部分 ADO.NET 结构的类。ADO.NET 结构使您可以生成可用于有效管理来自多个数据源的数据的组件。 System.Data.Common 包含由 .NET Framework 数据提供程序共享的类。.NET Framework 数据提供程序描述用于在托管空间中访问数据源(如数据库)的类的集合。 System.Data.Linq 包含支持在 LINQ to SQL 应用程序中与关系数据库进行交互的类。 System.Data.Linq.Mapping 包含用于生成表示关系数据库的结构和内容的 LINQ to SQL 对象模型的类。 System.Data.Mapping 提供用于存储数据映射信息的类型。 System.Data.Metadata.Edm 包含一组类型,这些类型表示模型中由实体框架使用的概念,以及一组帮助应用程序使用元数据的类。 System.Data.Objects 包含可访问对象服务的核心功能的类。 System.Data.Objects.DataClasses 包含以下这些类:Entity Data Model (EDM) 中定义的类型的基类、由导航属性返回的类型的基类,以及用于定义将公共语言运行时 (CLR) 对象映射到概念模型中类型的特性的类。 System.Data.Sql 包含支持特定于 SQL Server 的功能的类。此类的 API 扩展已添加到 SQL Server 的 .NET Framework 数据提供程序 ( System.Data.SqlClient) 中。 System.Data.SqlClient 包含封装 SQL Server .NET Framework 数据提供程序的类。SQL Server .NET Framework 数据提供程序描述了用于在托管空间中访问 SQL Server 数据库的类集合。 System.IO 包含允许对数据流和文件进行同步和异步读写的类型。 System.Linq 包含支持使用语言集成查询 (LINQ) 的查询的类和接口。 System.Web 提供启用浏览器/服务器通信的类和接口。 ……

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧