在这篇文章中,我们将带领您了解c#–Linq-to-entities,在一个查询中获取结果行数的全貌,包括c#获取列表长度的相关情况。同时,我们还将为您介绍有关asp.net-mvc–错误:无法在LI
在这篇文章中,我们将带领您了解c# – Linq-to-entities,在一个查询中获取结果行数的全貌,包括c#获取列表长度的相关情况。同时,我们还将为您介绍有关asp.net-mvc – 错误:无法在LINQ to Entities查询中构造实体或复杂类型、c# – linq exception:只能从LINQ to Entities调用此函数、c# – LINQ to Entities DateTime不一致、c# – LINQ to Entities中的LastIndexOf的知识,以帮助您更好地理解这个主题。
本文目录一览:- c# – Linq-to-entities,在一个查询中获取结果行数(c#获取列表长度)
- asp.net-mvc – 错误:无法在LINQ to Entities查询中构造实体或复杂类型
- c# – linq exception:只能从LINQ to Entities调用此函数
- c# – LINQ to Entities DateTime不一致
- c# – LINQ to Entities中的LastIndexOf
c# – Linq-to-entities,在一个查询中获取结果行数(c#获取列表长度)
基本思想是填充gridview并创建自定义分页.所以,我也需要结果和行数.
在sql中,这将是这样的:
SELECT COUNT(id),Id,Name... FROM ... WHERE ...
在简单的查询中获取所有内容.但是,我想保持一致并使用Linq2Entities.
到目前为止,我正在使用带有两个查询的方法(针对sql server),因为它只是起作用.我想优化它,而是使用单个查询.
我试过这个:
var query = from o in _db.Products select o; var prods = from o in query select new { Count = query.Count(),Products = query };
这产生了一个非常讨厌和长的查询与真正不必要的交叉连接和其他我不需要或想要的东西.
有没有办法在一个简单的查询中获取所有实体的分页结果计数?这里推荐的方法是什么?
更新:
刚试过FutureQueries,我做错了,或者它实际上执行了两个查询.这显示了我的sql profiler:
-- Query #1 SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[Products] AS [Extent1] WHERE 1 = [Extent1].[CategoryID] ) AS [GroupBy1];
下一行:
-- Query #1 SELECT [Extent1].[ID] AS [ID],[Extent1].[Name] AS [Name],[Extent1].[Price] AS [Price],[Extent1].[CategoryID] AS [CategoryID] FROM [dbo].[Products] AS [Extent1] WHERE 1 = [Extent1].[CategoryID];
C#代码:
internal static List<Product> GetProducts(out int _count) { DatabaseEntities _db = new DatabaseEntities(); var query = from o in _db.Products where o.CategoryID == 1 select o; var count = query.FutureCount(); _count = count.Value; return query.Future().ToList(); }
我错过了什么?根据我的分析器,除了在查询中添加了行( – 查询#1)之外,它完全相同.
解决方法
var q = db.Products.Where(p => ...); var qCount = q.FutureCount(); var qPage = q.Skip((pageNumber-1)*pageSize).Take(pageSize).Future(); int total = qCount.Value; // Both queries are sent to the DB here. var tasks = qPage.ToList();
asp.net-mvc – 错误:无法在LINQ to Entities查询中构造实体或复杂类型
The entity or complex type ‘Tusofona_Website.Models.site_noticias’ cannot be constructed in a LINQ to Entities query.
我的控制器:
private TusofonaDBs db = new TusofonaDBs(); // // GET: /DestaquesMain/ public ActionResult Index() { var query = (from sd in db.site_desquesnoticias join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia where sn.Destaque == 1 select new site_noticias { CorpoNoticia = sn.CorpoNoticia,TituloNoticia = sn.TituloNoticia }).ToList(); //return View(db.site_desquesnoticias.ToList()); return View(query); }
我的型号:
public class site_destaquesnoticias { [Key] public Int32 IDDestaque { get; set; } public Int32 IDNoticia { get; set; } public string Foto { get; set; } } public class site_noticias { [Key] public Int32 IDNoticia { get; set; } public string CorpoNoticia { get; set; } public string TituloNoticia { get; set; } public string Foto { get; set; } public Int32 Destaque { get; set; } } public class TusofonaDBs : DbContext { public DbSet<site_destaquesnoticias> site_desquesnoticias { get; set; } public DbSet<site_noticias> site_noticias { get; set; } }
有人可以帮帮我吗?
解决方法
但是,您可以做以下几件事:
1)选择匿名类型而不是实体,如:
var query = (from sd in db.site_desquesnoticias join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia where sn.Destaque == 1 select new { CorpoNoticia = sn.CorpoNoticia,TituloNoticia = sn.TituloNoticia }).ToList();
2)反转您的查询以直接选择site_noticias.这取决于您要检索的查询和数据.例如,您可以查看以下内容是否有效并为您提供所需的数据:
var query = (from sd in db.site_desquesnoticias join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia where sn.Destaque == 1 select sn).ToList();
3)使用一些DTO(数据传输对象)将要选择的属性投影到:
public class SiteNoticiasDTO{ public string CorpoNoticia {get;set;} public string TituloNoticia {get;set;} } var query = (from sd in db.site_desquesnoticias join sn in db.site_noticias on sd.IDNoticia equals sn.IDNoticia where sn.Destaque == 1 select new SiteNoticiasDTO { CorpoNoticia = sn.CorpoNoticia,TituloNoticia = sn.TituloNoticia }).ToList();
c# – linq exception:只能从LINQ to Entities调用此函数
List<ExecutionLog3> reportserverDB = UpdateCache(); var reportLog = (from r in reportserverDB orderby r.TimeStart descending where ((model.reportName == null ? true : r.ItemPath.Contains(model.reportName)) && (model.reportFolder == null ? true : r.ItemPath.Contains(model.reportFolder)) && (r.TimeStart >= startDateTime) && (r.TimeStart <= endDateTime) ) select new FilteRSSrsLog { UserName = r.UserName,ReportName = r.ItemPath,ReportFolder = r.ItemPath,Format = r.Format,Parameters = r.Parameters,TimeStart = r.TimeStart,TimeEnd = r.TimeEnd,TotalTime = EntityFunctions.DiffMilliseconds(r.TimeStart,r.TimeEnd) });
如果我删除“选择新的FilteRSSrsLog”代码块并写入“select r”它可以工作.但我只需要那些颜色,那么我该怎么做才能解决这个问题呢?
解决方法
如果要在内存中运行此查询,请替换
TotalTime = EntityFunctions.DiffMilliseconds(r.TimeStart,r.TimeEnd)
同
TotalTime = (r.TimeEnd - r.TimeStart).TotalMilliseconds
减去两个日期会产生一个TimeSpan
值,您可以从中获取其TotalMilliseconds
属性.
c# – LINQ to Entities DateTime不一致
DateTime date = DateTime.Now; DateTest newRecord = new DateTest { dateColumn = date }; db.DateTest.Addobject(newRecord); db.SaveChanges(); IQueryable<DateTest> records = from d in db.DateTest select d;
如果我此时破坏代码,并查看调试器中的对象,我得到日期对象:
Date {11/22/2011 12:00:00 AM} System.DateTime Day 22 int DayOfWeek Tuesday System.DayOfWeek DayOfYear 326 int Hour 8 int Kind Local System.DateTimeKind Millisecond 345 int Minute 59 int Month 11 int Second 33 int Ticks 634575491733450602 long TimeOfDay {08:59:33.3450602} System.TimeSpan Year 2011 int
我从表中检索到的记录得到了这个:
Date {11/22/2011 12:00:00 AM} System.DateTime Day 22 int DayOfWeek Tuesday System.DayOfWeek DayOfYear 326 int Hour 8 int Kind Unspecified System.DateTimeKind Millisecond 347 int Minute 59 int Month 11 int Second 33 int Ticks 634575491733470000 long TimeOfDay {08:59:33.3470000} System.TimeSpan Year 2011 int
正如你所看到的,它们会在几毫秒内完成.
任何人都可以解释为什么这样,以及我如何解决它?我需要能够查询与内存中的DateTime对象完全匹配的记录,但是这种行为导致我的查询空手而归.
解决方法
来自MSDN – datetime (Transact-SQL):
Accuracy: Rounded to increments of .000,.003,or .007 seconds
因此,在您的情况下,毫秒会向上舍入到.007,给出.3470000而不是.3450602.
DateTime2
sql Server数据类型的分辨率为100纳秒,与.NET类似,因此可能是合适的替代品.
c# – LINQ to Entities中的LastIndexOf
var data = db.GetEntities(). OrderBy(e=>e.StringProperty.LastIndexOf("Foo")).ToList()
但是,LINQ to Entities不支持LastIndexOf.我试过寻找类似的问题,但我发现的只有this,这并没有解决我的问题(订购).在MSDN上搜索没有产生任何结果.
使用LINQ to Entities完成此操作的最简单方法是什么(我不希望在ToList()之后执行此操作).
解决方法
OrderBy(e => e.StringProperty.Length - EntityFunctions.Reverse(e.StringProperty).IndexOf("ooF"))
我认为支持Reverse和IndexOf.
关于c# – Linq-to-entities,在一个查询中获取结果行数和c#获取列表长度的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于asp.net-mvc – 错误:无法在LINQ to Entities查询中构造实体或复杂类型、c# – linq exception:只能从LINQ to Entities调用此函数、c# – LINQ to Entities DateTime不一致、c# – LINQ to Entities中的LastIndexOf的相关信息,请在本站寻找。
本文标签: