gpt4 book ai didi

c# - Linq 查询无法合并两个表记录

转载 作者:行者123 更新时间:2023-11-29 10:28:46 24 4
gpt4 key购买 nike

我正在尝试使用 Concat 合并两个 linq 查询。但是当我编译查询时,我收到两个错误。

IQueryable”不包含“Concat”的定义,并且最佳扩展方法重载“ParallelEnumerable.Concat(ParallelQuery, IEnumerable)”需要“ParallelQuery”类型的接收器

第二个错误是方法所有代码未返回所有路径值

这是我的界面。

[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**


}

}

最佳答案

您不需要使用 2 个单独的查询,只需通过连接 2 个表构建单个查询并从结果集中选择所有必需的属性即可。然后,您可以将其放入 IEnumerable 对象中并使用 JSON serializer从查询结果返回 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
}
}

关于c# - Linq 查询无法合并两个表记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com