我正在将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
}
}
}
}
这是我运行应用程序并输入帐号1时的屏幕截图,它有两个表及其能够显示的记录。
这是我输入帐号15时的屏幕截图,该帐号只有Current_Account_Withdraw表的记录,没有Current_Account_Deposit表的记录,并且它没有显示任何内容。
然后我改变它。
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
}
}
这是我编译时得到的错误。 'List <>'不包含'Union'的定义,最好的扩展方法重载'Queryable.Union <>(IQueryable <>,IEnumerable <>)'需要一个'IQueryable <>'类型的接收器
您的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,
}
您的实际解决方案将取决于您实际想要默认的值。