简体   繁体   English

如何解决IQueryable Select语句中的Union上的错误

[英]How do I resolve an error on Union within IQueryable Select statement

I wrote an IQueryable select statement and tried declare a union, the reason for this union is because we are going to include code later that does some magic and we found a union to be the most appropriate method however I'll not get into that as it's nothing to do with this error. 我写了一个IQueryable select语句并尝试声明一个并集,之所以这样做,是因为我们稍后将包含执行一些魔术的代码,并且我们发现并集是最合适的方法,但是我不会那样做。与该错误无关。

The error returned by Visual Studio is Visual Studio返回的错误是

Error   6   
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 
'System.Linq.IQueryable<MyProject.Models.tbl_customers>'. An  explicit conversion exists (are you missing a cast?)

The select statement is: select语句为:

var datacontext = db.tbl_customers.AsQueryable();

IQueryable<CustomerViewModel> thecustomer = (
               from p in datacontext
               select new {
                   customer_id = p.vessel_idx,
                   customer_name = p.customer_name                                                         
               })
               .Union (( 
                   from p in datacontext
                   select new {
                       customer_id = p.vessel_idx,
                       customer_name = p.customer_name                                                           
               }));

I tried adding .ToList but this didn't help. 我尝试添加.ToList,但这没有帮助。 Can anyone suggest a way to fix this? 谁能建议解决此问题的方法?

Agreed with @har07, both queries project the result using the same anonymous type (they share the same property names and types). 同意@ har07,这两个查询使用相同的匿名类型(它们共享相同的属性名称和类型)来投影结果。 The problem is thecustomer variable which is IQueryable<CustomerViewModel> . 问题是thecustomer变量,它是IQueryable<CustomerViewModel> Another way of solve this problem is projecting your queries to your ViewModel class: 解决此问题的另一种方法是将查询投影到ViewModel类:

IQueryable<CustomerViewModel> thecustomer = (
               from p in datacontext
               select new CustomerViewModel{
                   customer_id = p.vessel_idx,
                   customer_name = p.customer_name                                                         
               })
               .Union (( 
                   from p in datacontext
                   select new CustomerViewModel{
                       customer_id = p.vessel_idx,
                       customer_name = p.customer_name                                                           
               }));

In this case I'm assuming your view model class has properties with the same names of your anonymous type, but that's something you can change at your convenience. 在这种情况下,我假设您的视图模型类具有与匿名类型相同的名称的属性,但是您可以在方便时进行更改。

Well.. I suspect this has nothing to do with the fact that your LINQ uses Union() . 好吧..我怀疑这与您的LINQ使用Union()无关。 It is because the LINQ returns anonymous type while the variable devoted to hold the result was declared of type IQueryable<CustomerViewModel> . 这是因为LINQ返回了匿名类型,而专用于保存结果的变量被声明为IQueryable<CustomerViewModel>类型。

You could fix it easily by changing the thecustomer variable declaration to a suitable type, for example, var or IQueryable<dynamic> or something else capable of holding IQueryable of anonymous type value : 您可以通过将thecustomer变量声明更改为合适的类型(例如varIQueryable<dynamic>或其他能够保存匿名类型值的IQueryable的类型)来轻松修复它:

var thecustomer = (
               from p in datacontext
               select new {
                   customer_id = p.vessel_idx,
                   customer_name = p.customer_name                                                         
               })
               .Union (( 
                   from p in datacontext
                   select new {
                       customer_id = p.vessel_idx,
                       customer_name = p.customer_name                                                           
               }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM