امکان انجام محاسبات سمت کلاینت در EF Core
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۶/۲۴ ۱۳:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
var blogs = from blog in Blogs
where blog.Name.Contains("Development")
select blog; var blogs = from blog in Blogs where blog.Name.ComputeHash() == 0 select blog;
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
} public static class StringExtensions
{
public static int ComputeHash(this string str)
{
var hash = 0;
foreach (var ch in str)
{
hash += (int)ch;
}
return hash;
}
} using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Where(blog => blog.Url.ComputeHash() >= 10)
.ToList();
Console.WriteLine(blogs.First().Url);
} SELECT [blog].[BlogId], [blog].[Url] FROM [Blogs] AS [blog]
var idUrls = context.Blogs
.Select(b => new
{
IdUrlString = string.Join(", ", b.BlogId, b.Url),
}).ToList(); public class BloggingContext : DbContext
{
public BloggingContext()
{ }
public BloggingContext(DbContextOptions options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Demo.ClientSideEvaluation;Trusted_Connection=True;");
optionsBuilder.ConfigureWarnings(warnings =>
{
warnings.Log(CoreEventId.IncludeIgnoredWarning);
warnings.Log(RelationalEventId.QueryClientEvaluationWarning);
});
}
}
} warn: Microsoft.EntityFrameworkCore.Query[200500] The LINQ expression 'where ([blog].Url.ComputeHash() >= 10)' could not be translated and will be evaluated locally.
var idUrls2 = context.Blogs
.Select(b => new
{
IdUrlString = b.BlogId + "," + b.Url
}).ToList(); SELECT (CAST([b].[BlogId] AS nvarchar(max)) + N',') + [b].[Url] AS [IdUrlString] FROM [Blogs] AS [b]
var test1 = context.Blogs .Where(blog => String.Compare(blog.Url, "A", StringComparison.Ordinal) > 0) .ToList(); // SELECT [blog].[BlogId], [blog].[Url] // FROM [Blogs] AS [blog]
var test2 = context.Blogs .Where(blog => String.Compare(blog.Url, "B") > 0) .ToList(); // SELECT [blog].[BlogId], [blog].[Url] // FROM [Blogs] AS [blog] // WHERE [blog].[Url] > N'B'
var test3 = context.Blogs
.Where(blog => blog.Url.Equals("C", StringComparison.OrdinalIgnoreCase))
.ToList();
// SELECT [blog].[BlogId], [blog].[Url]
// FROM [Blogs] AS [blog] var test3_1 = context.Blogs
.Where(blog => blog.Url.Equals("C_1"))
.ToList();
// SELECT [blog].[BlogId], [blog].[Url]
// FROM [Blogs] AS [blog]
// WHERE [blog].[Url] = N'C_1' var test4 = context.Blogs
.Where(blog => blog.Url.StartsWith("D"))
.ToList();
// SELECT [blog].[BlogId], [blog].[Url]
// FROM [Blogs] AS [blog]
// WHERE [blog].[Url] LIKE N'D' + N'%' AND (LEFT([blog].[Url], LEN(N'D')) = N'D') var test5 = context.Blogs .Where(blog => EF.Functions.Like(blog.Url, "S_i%")) .ToList(); // SELECT [blog].[BlogId], [blog].[Url] // FROM [Blogs] AS [blog] // WHERE [blog].[Url] LIKE N'S_i%'
var test6 = context.Blogs .Where(blog => blog.Url.ToUpper() == "E") .ToList(); // SELECT [blog].[BlogId], [blog].[Url] // FROM [Blogs] AS [blog] // WHERE UPPER([blog].[Url]) = N'E'
var test7 = context.Blogs .Where(blog => blog.Url.ToUpperInvariant() == "F") .ToList(); // SELECT [blog].[BlogId], [blog].[Url] // FROM [Blogs] AS [blog]