联合运营商的Linq查询返回错误

时间:2017-12-18 01:35:21

标签: c# angularjs linq wcf

我正在将wcf服务用于angular js web应用程序。我有两个表。我将两个表记录连接到单个记录并在角度js应用程序中显示记录。当我有两个表的记录时,它能够检索但是如果我在数据库中只有一个表记录,则它不会显示以下查询的任何内容。

 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 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 == accountNumber //Modify it
                                     select new
                                     {
                                         x.Account_Number,
                                         x.Account_Holder_Name,
                                         x.Transcation_Type,
                                         x.Amount,
                                         Transcation_Type1 = y.Transcation_Type,
                                         Amount1 = y.Amount,

                                         // put other properties here 
                                     }).ToList();

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();

                return js.Serialize(CombinedQuery); // return JSON string
            }

        }  
    }
}

这是数据库记录。 enter image description here

以下是我运行应用程序并输入帐号1时的屏幕截图,它有两个表及其能够显示的记录。 enter image description here

这是我输入帐号15时的屏幕截图,该帐号只有Current_Account_Withdraw表的记录,没有Current_Account_Deposit表的记录,并且它没有显示任何内容。 enter image description here

然后我改变它。

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 DepsoitQuery = (from x in context.Current_Account_Deposit
                                 join y in context.Current_Account_Withdraw
                                 on x.Account_Number equals y.Account_Number
                                 into JoinedList
                                 from y in JoinedList.DefaultIfEmpty()
                                 where x.Account_Number == accountNumber //Modify it
                                 select new
                                 {
                                     x.Account_Number,
                                     x.Account_Holder_Name,
                                     x.Amount,

                                     // put other properties here 
                                 }).ToList();

            var withdrawQuery = (from y in context.Current_Account_Deposit
                                 join x in context.Current_Account_Withdraw
                                 on y.Account_Number equals x.Account_Number
                                 into JoinedList
                                 from x in JoinedList.DefaultIfEmpty()
                                 where x.Account_Number == accountNumber
                                 select new
                                 {
                                     y.Amount,
                                 }).ToList();

            var CombinedQuery = DepsoitQuery.Union(withdrawQuery).ToList();//**error on this line**

            var js = new System.Web.Script.Serialization.JavaScriptSerializer();

            return js.Serialize(CombinedQuery); // return JSON string
        }

    }  

这是我编译时遇到的错误。     的'列表与LT;>'不包含' Union'的定义和最好的扩展方法重载' Queryable.Union<>(IQueryable<>,IEnumerable<>)'需要一个类型为“IQueryable<>'

的接收器

1 个答案:

答案 0 :(得分:1)

您的DepositQuery是根据此匿名类型生成的列表:

select new
{
    x.Account_Number,
    x.Account_Holder_Name,
    x.Amount,
}

您的withdrawQuery是根据此匿名类型生成的列表:

select new
{
    y.Amount,
}

这些类型不兼容。 (你可以"联合"例如一个字符串和一个整数,或者两个单独的类 - 当你有两个完全不同的匿名类型时,这就是你正在做的事情)你需要制作如果您希望能够一起使用它们,则两种类型都声明相同类型的相同属性。

例如,您可以将第二个匿名类型声明为:

select new
{
    Account_Number = 0,
    Account_Holer_Name = "Unknown",
    y.Amount,
}

您的实际解决方案将取决于您实际想要默认的值。