【问题标题】:Linq Query Failed to Merge Two Tables RecordLinq 查询合并两个表记录失败
【发布时间】:2018-05-29 06:07:40
【问题描述】:

我正在尝试使用 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**


            }

        }  

【问题讨论】:

  • var merge = DepositQuery.Union(WithdrawQuery); (如果Current_Account_DepositCurrent_Account_Withdraw 具有完全相同的结构,则可用)。您还需要为TranscationDetails 方法返回一个字符串(目前它缺失,或者如果您返回查询结果使用IQueryable)。
  • 使用Union() 方法得到什么结果?要执行Union,两个查询结果必须具有相同的列列表。如果两个结果集不同,则使用ToList() 创建IEnumerable 对象,然后使用Union()
  • 我想从 Current_Account_Deposit 和 Current_Account_Withdraw 表中选择所有带有帐号的记录,然后将它们合并到单个查询中
  • 尝试使用Join方法:from x in context.Current_Account_Deposit join y in context.Current_Account_Withdraw on x.AccountNumber equals y.AccountNumber where x.Account_Number == Convert.ToInt32(Account_Number) select ...;
  • Join 方法已经奏效了吗?你想要TranscationDetails 方法的返回类型到底是什么(我假设你想要IQueryable,但你将它定义为字符串)?

标签: c# mysql entity-framework linq


【解决方案1】:

您不需要使用 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
     }
}

【讨论】:

  • 我必须使用哪些表属性??是往来账户存款还是往来账户取款??
  • 在两个表中我都有账号、交易类型、金额、账户持有人姓名等属性。
  • 这两个表属性都可以使用,这取决于您的需要。如果您想从两个表中返回所有结果,也可以使用select new { x, y }
  • 我要返回账号、账户持有人姓名、交易类型、活期存款金额和交易类型以及活期提款金额。
  • 那么你可以使用select new { x.AccountNumber, x.AccountHolderName, x.TransactionType, x.Amount, y.TransactionType, y.Amount }。请注意,由于 2 列组 (TransactionType & Amount) 对两个表具有相同的名称,您可能需要额外的命名来区分它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
相关资源
最近更新 更多