Linq查询返回匿名类型错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq查询返回匿名类型错误相关的知识,希望对你有一定的参考价值。
我试图开发银行系统。我试图将三个表记录连接到单个表中。但问题是当我编译它时我得到了以下错误。
错误CS1929'IOrderedQueryable <>'不包含'Concat'的定义,并且最好的扩展方法重载'ParallelEnumerable.Concat <>(ParallelQuery <>,IEnumerable <>)'需要类型为'ParallelQuery <>的接收器
这是我的Linq查询。
public string TranscationDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Deposit.Where(x => x.Account_Number == accountNumber).Select(w => new
{
w.Account_Number,
Deposit = (decimal?)null,
Withdrawal = (decimal?)w.Amount,
w.Date
}).Concat(context.Current_Account_Withdraw.Select(d => new
{
d.Account_Number,
Deposit = (decimal?)d.Amount,
Withdrawal = (decimal?)null,
d.Date
})).OrderBy(r => r.Date)
.Concat(context.Current_Account_Details.Select(e => new
{
//You should perform same anonymous type which you want to concat
Account_Number = e.Account_Number,
Deposit = (decimal?)e.Account_Balance,
Withdrawal = (decimal?)null,
e.Account_Fees
}));
var js = new System.Web.Script.Serialization.javascriptSerializer();
return js.Serialize(inOut); // return JSON string
}
}
}
This is DBContext class.
public partial class HalifaxDatabaseEntities : DbContext
{
public HalifaxDatabaseEntities()
: base("name=HalifaxDatabaseEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Web_User_login> Web_User_login { get; set; }
public virtual DbSet<USER> USERS { get; set; }
public virtual DbSet<tblUser> tblUsers { get; set; }
public virtual DbSet<Current_Account_Holder_Details> Current_Account_Holder_Details { get; set; }
public virtual DbSet<Current_Account_Details> Current_Account_Details { get; set; }
public virtual DbSet<Current_Account_Deposit> Current_Account_Deposit { get; set; }
public virtual DbSet<Current_Account_Withdraw> Current_Account_Withdraw { get; set; }
}
答案
修改你的代码,为Concat
的每个参与者创建相同的模式/类,因为两个匿名类型永远不会相同,因此它总是会失败,Concat
需要相同/一致的类型T
,检查下面的详细信息:
IEnumerable<T>
需要IEnumerable<T>
用于Concat调用,因此类型T
保持一致,这对于匿名类型是不可能的,检查定义here,它像:
public static IEnumerable<TSource> Concat<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second
)
class Account
{
public int? Account_Number {get;set;}
public decimal? Deposit {get;set;}
public decimal? Withdrawal {get;set;}
public decimal? Account_Balance {get;set;}
public DateTime? Date {get;set;}
}
public string TranscationDetails(string Account_Number)
{
var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var inOut = context.Current_Account_Deposit.Select(w => new Account
{
Account_Number = w.Account_Number
,
Deposit = (decimal?)null,
Withdrawal = (decimal?)w.Amount
,
Date = w.Date
}).Concat(context.Current_Account_Withdraw.Select(d => new Account
{
Account_Number = d.Account_Number
,
Deposit = (decimal?)d.Amount
,
Withdrawal = (decimal?)null
,
Date = d.Date
})).OrderBy(r => r.Date)
.Concat(context.Current_Account_Details.Select(e => new Account
{
Account_Number = e.Account_Number,
Account_Balance = (decimal?)e.Account_Balance
}));
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(inOut); // return JSON string
}
}
另一答案
错误很清楚。你正在尝试Concat
不同的匿名类型。
var inOut = context.Current_Account_Deposit.Select(w => new
{
Account_Number = w.Account_Number
,
Deposit = (decimal?)null,
Withdrawal = (decimal?)w.Amount
,
Date = w.Date
}).Concat(context.Current_Account_Withdraw.Select(d => new
{
Account_Number = d.Account_Number
,
Deposit = (decimal?)d.Amount
,
Withdrawal = (decimal?)null
,
Date = d.Date
})).OrderBy(r => r.Date)
.Concat(context.Current_Account_Details.Select(e => new
{
//You should perform same anonymous type which you want to concat
Account_Number = e.Account_Number,
Deposit = (decimal?)e.Account_Balance,
Withdrawal = (decimal?)null,
Date = null
}));
此外,我不知道要求和细节,但你确定使用Concat
?看来你需要创建join
而不是Concat
。如果实体具有导航属性,我强烈建议您使用它们。
以上是关于Linq查询返回匿名类型错误的主要内容,如果未能解决你的问题,请参考以下文章
在 LINQ 查询中将 KeyValuePair 转换为匿名类型