联合运营商Linq查询返回错误
我正在使用wcf服务转换为角色js web应用程序。我有两个表。我将两个表记录加入单个记录并在角度js应用程序中显示记录。当我有记录两个表,它能够检索,但如果我只有一个数据库表记录,它不同意这种下面的查询显示任何东西..联合运营商Linq查询返回错误
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
}
}
这是我编译时得到的错误。 '列出<>' 不包含 '联盟' 的定义和最佳的扩展方法过载 '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,
}
您的实际解决方案将取决于什么值,你真的想默认。
以上是 联合运营商Linq查询返回错误 的全部内容, 来源链接: utcz.com/qa/260415.html