我正在尝试开发银行系统。我试图将三个表记录连接到单个表中。但问题是当我编译它时我得到了以下错误。
错误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; }
}
答案 0 :(得分:2)
错误很清楚。您正尝试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
。如果实体具有导航属性,我强烈建议您使用它们。
答案 1 :(得分:1)
修改代码,为Concat
的每个参与者创建与输出相同的模式/类,这里因为两个匿名类型永远不会相同,因此它总是会失败,Concat
需要相同/一致输入T
,查看下方的详细信息:
IEnumerable<T>
需要IEnumerable<T>
,因此类型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
}
}