نکاتی در مورد استفاده از توابع تجمعی در Entity framework
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۷/۱۱ ۱۰:۳۸
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Bill
{
public int Id { set; get; }
public string Name { set; get; }
public virtual ICollection<Transaction> Transactions { set; get; }
}
public class Transaction
{
public int Id { set; get; }
public DateTime AddDate { set; get; }
public int Amount { set; get; }
[ForeignKey("BillId")]
public virtual Bill Bill { set; get; }
public int BillId { set; get; }
}
public class MyContext : DbContext
{
public DbSet<Bill> Bills { get; set; }
public DbSet<Transaction> Transactions { get; set; }
}
public class Configuration : DbMigrationsConfiguration<MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyContext context)
{
var bill1 = new Bill { Name = "bill-1" };
context.Bills.Add(bill1);
for (int i = 0; i < 11; i++)
{
context.Transactions.Add(new Transaction
{
AddDate = DateTime.Now.AddDays(-i),
Amount = 1000000000 + i,
Bill = bill1
});
}
base.Seed(context);
}
}
public static class Test
{
public static void RunTests()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
using (var context = new MyContext())
{
var sum = context.Transactions.Sum(x => x.Amount);
Console.WriteLine(sum);
}
}
}
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
SUM([Extent1].[Amount]) AS [A1]
FROM [dbo].[Transactions] AS [Extent1]
) AS [GroupBy1]
Arithmetic overflow error converting expression to data type int.
var sum2 = context.Transactions.Sum(x => (Int64)x.Amount);
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
SUM( CAST( [Extent1].[Amount] AS bigint)) AS [A1]
FROM [dbo].[Transactions] AS [Extent1]
) AS [GroupBy1]
var date = DateTime.Now.AddDays(10);
var sum3 = context.Transactions
.Where(x => x.AddDate > date)
.Sum(x => (Int64)x.Amount);
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
SUM( CAST( [Extent1].[Amount] AS bigint)) AS [A1]
FROM [dbo].[Transactions] AS [Extent1]
WHERE [Extent1].[AddDate] > @p__linq__0
) AS [GroupBy1]
The cast to value type 'Int64' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
var date = DateTime.Now.AddDays(10);
var sum3 = context.Transactions
.Where(x => x.AddDate > date)
.Sum(x => (Int64?)x.Amount) ?? 0;
var sum4 = context.Bills.First().Transactions.Sum(x => (Int64?)x.Amount) ?? 0;
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[AddDate] AS [AddDate],
[Extent1].[Amount] AS [Amount],
[Extent1].[BillId] AS [BillId]
FROM [dbo].[Transactions] AS [Extent1]
WHERE [Extent1].[BillId] = @EntityKeyValue1
var entry = context.Bills.First(); var sum5 = context.Entry(entry).Collection(x => x.Transactions).Query().Sum(x => (Int64?)x.Amount) ?? 0;
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
SUM( CAST( [Extent1].[Amount] AS bigint)) AS [A1]
FROM [dbo].[Transactions] AS [Extent1]
WHERE [Extent1].[BillId] = @EntityKeyValue1
) AS [GroupBy1]