C# Linq查询无法合并两个记录表
我正在尝试使用Concat合并两个linq查询。但是当我编译查询时,我收到两个错误 IQueryable'不包含“Concat”的定义,并且最佳扩展方法重载“parallelenumable.Concat(ParallelQuery,IEnumerable)”需要“ParallelQuery”类型的接收器 第二个错误是方法所有代码不返回所有路径值 这是我的界面C# Linq查询无法合并两个记录表,c#,mysql,entity-framework,linq,C#,Mysql,Entity Framework,Linq,我正在尝试使用Concat合并两个linq查询。但是当我编译查询时,我收到两个错误 IQueryable'不包含“Concat”的定义,并且最佳扩展方法重载“parallelenumable.Concat(ParallelQuery,IEnumerable)”需要“ParallelQuery”类型的接收器 第二个错误是方法所有代码不返回所有路径值 这是我的界面 [OperationContract] [WebInvoke(Method = "GET", RequestFo
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
//BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/TranscationDetails/{Account_Number}")]
string TranscationDetails(string Account_Number);
下面是实现
public string TranscationDetails(string Account_Number)
{
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var DepositQuery = from x in context.Current_Account_Deposit
where x.Account_Number == Convert.ToInt32(Account_Number)
select x;
var WithdrawQuery = from x in context.Current_Account_Withdraw
where x.Account_Number == Convert.ToInt32(Account_Number)
select x;
var merge = DepositQuery.Concat(WithdrawQuery);//**Error on this line**
}
}
您不需要使用两个单独的查询,只需通过连接两个表来构建单个查询,并从结果集中选择所有必需的属性。然后,您可以将其放入
IEnumerable
对象中,并使用它从查询结果返回JSON响应:
public string TranscationDetails(string Account_Number)
{
using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
{
var CombinedQuery = (from x in context.Current_Account_Deposit
join y in context.Current_Account_Withdraw
on x.Account_Number equals y.Account_Number
where x.Account_Number == Convert.ToInt32(Account_Number)
select new {
x.Account_Number,
// put other properties here
}).ToList();
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
return js.Serialize(CombinedQuery); // return JSON string
}
}
var merge=depostQuery.Union(撤销查询)代码>(如果活期存款
和活期存款
具有完全相同的结构,则可用)。您还需要为TransactionDetails
方法返回一个字符串(当前缺少该字符串,或者如果您返回查询结果,请使用IQueryable
)。使用Union()
方法会得到什么结果?要执行联合,两个查询结果必须具有相同的列列表。如果两个结果集不同,请使用ToList()
创建IEnumerable
对象,然后使用Union()
。我想从活期存款和活期取款表中选择所有记录,并使用Join
方法:从上下文中的x开始。活期存款在上下文中加入y。活期存款在x开始取款。AccountNumber等于y。AccountNumber其中x、 帐号==转换为32(帐号)选择
那个Join
方法已经工作了吗?对于transactiondetails
方法(我假设您想要IQueryable
但您将其定义为字符串),您想要什么样的返回类型?我必须使用哪些表属性??是活期存款还是活期存款?在这两个表中,我都有一些属性,如账号、交易类型、金额、账户持有人姓名等。根据您的需要,可以使用这两个表的属性。如果要返回两个表中的所有结果,也可以使用select new{x,y}
。我要返回账号、账户持有人姓名、交易类型、活期存款金额和交易类型以及活期账户取款金额。然后可以使用select new{x.AccountNumber,x.AccountHolderName,x.TransactionType,x.Amount,y.TransactionType,y.Amount}
。请注意,由于两个列组(TransactionType
&Amount
)对两个表具有相同的名称,因此可能需要额外的命名来区分它们。