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> 了?有什么方法解决这个问题吗?
...全文
208 14 打赏 收藏 转发到动态 举报
写回复
用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

110,896

社区成员

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

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

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