linq-to-entities

Combine Predicates in Linq-to-entities

时光总嘲笑我的痴心妄想 提交于 2019-12-19 19:49:04
问题 I want to dynamically build my list of where conditions. Here's a snippet of my code: protected Expression<Func<event_info, bool>> _wherePredicate = c => true; public void main() { _wherePredicate = _wherePredicate.And(c => c.createdby == 6); _wherePredicate = _wherePredicate.And(c => c.isdeleted == 0); var query = from ev in dataConnection.event_info where ev.isdeleted == 0 select ev; Results = query.Where(_wherePredicate).ToList(); } Except this doesn't work because linq-to-entities doesn't

EF - DistinctBy on an IQueryable?

≡放荡痞女 提交于 2019-12-19 14:51:11
问题 Say I have this where I want to get every person in group 42, but I want to get an IQueryable of each unique first name in the database (PS - I know I can call AsQueryable() after the Select but that's not what I'm interested in doing - I want the database to perform the distinct, not the program): MyEfContextContainer db = new MyEfContextContainer(); IQueryable<string> uniqueFirstNames = db.People .Where(x=> x.GroupId == 42) .DistinctBy(c=> c.FirstName) .Select(c=> c.FirstName); From what I

LINQ to Entities does not recognize the method 'System.String get_Item (System.String)',

南楼画角 提交于 2019-12-19 12:52:13
问题 How can I solve this problem? Here is my code: DateTime dtInicio = new DateTime(); DateTime dtFim = new DateTime(); Int32 codStatus = 0; if(!string.IsNullOrEmpty(collection["txtDtInicial"])) dtInicio = Convert.ToDateTime(collection["txtDtInicial"]); if(!string.IsNullOrEmpty(collection["txtDtFinal"])) dtFim = Convert.ToDateTime(collection["txtDtFinal"]); if (!string.IsNullOrEmpty(collection["StatusCliente"])) Convert.ToInt32(collection["StatusCliente"]); var listCLientResult = (from c in db

LINQ to Entities select all entries in many-to-many relationship

你。 提交于 2019-12-19 11:39:13
问题 I have 3 MySql tables: Students , Classes and StudentsInClasses . The Entity Framework translates these into two entities Student and Class , each linking to the other with a many-to-many navigation property (e.g. Student.Classes ). But there is no StudentsInClasses entity, so what's the best way to call, using LINQ to Entities, the equivalent of SQL : SELECT StudentId, ClassId FROM StudentsInClasses; I'm looking for a HashSet (or equivalent) of { StudentId, ClassId } pairs so I can quickly

Linq dynamic queries for user search screens

两盒软妹~` 提交于 2019-12-19 10:24:40
问题 I have a database that has a user search screen that is "dynamic" in that I can add additional search criteria on the fly based on what columns are available in the particular view the search is based on and it will allow the user to use them immediately. Previously I had been using nettiers for this database, but now I am programming a new application against it using RIA and EntFramework 4 and LINQ. I currently have 2 tables that are used for this, one that fills the combobox with the

Linq To Entities Compare Value Against List<int>

若如初见. 提交于 2019-12-19 10:22:39
问题 I am using Entity Framework 5.0 , and I have a problem with a LINQ query. I have the following method that accepts an integer value which is then passed into the query. This works fine. public IList<tblcoursebooking> GetStandardReport(int AttendanceID) { return _UoW.tblcoursebookingRepo.All .Where(cb => cb.Attended.Equals(AttendanceID) .ToList(); } However, I need to change the method so that it accepts a List of integers , and then pulls out all records where Attended is equal to any of the

LINQ to Entities does not recognize the method Generic.List(int) to Generic.IEnumerable(int) method

眉间皱痕 提交于 2019-12-19 08:54:02
问题 The database contains Orders. Orders can be contained within a group of Orders. For every group of Orders it could contain 1 to many Orders. However, Orders could have a NULL value assigned GroupOrderId as previous Orders did not have the grouping concept. Only new Orders enforce the concept of being added to a group. The class structure to be populated in order to perform actions on each Order is public class OrdersGroup { public int? GroupOrderId { get; set; } public List<int> OrderIds {

.Where(<condition>).FirstOrDefault() vs .FirstOrDefault(<condition>)

一世执手 提交于 2019-12-19 08:03:27
问题 Using Linq to Entities is there a difference between the following? db.EntityName.Where(a => a.Id == id).FirstOrDefault(); db.EntityName.FirstOrDefault(a => a.Id == id); Or is it simply a matter of personal preference? Thanks. 回答1: Both generate the same SQL statement. The second approach is shorter, while the first might be clearer to some developers. Ultimately it's a matter of personal preference. You can inspect the SQL using the ObjectQuery.ToTraceString method. 来源: https://stackoverflow

Conditional WHERE in LINQ

风流意气都作罢 提交于 2019-12-19 07:09:19
问题 Hi any suggestions on building a LINQ statement based on search criteria? I'll be passing in an instance of a 'SearchCriteria' class with all parameters nullable. I then want to if (sc.a != null) // add to where if (sc.b != null) // add to where The key thing is these are to be ORs not ANDs. Any tips? And for bonus points I'd like to use 'contains' on an int? but I can only get equals or not equals. 回答1: Try: .Where(x => (x.a != null ? x.a == a : false) && (x.b != null ? x.b == b : false));

Linq-to-Entities Left JOIN

寵の児 提交于 2019-12-19 06:30:45
问题 This is my query: from forum in Forums join post in Posts on forum equals post.Forum into postGroup from p in postGroup where p.ParentPostID==0 select new { forum.Title, forum.ForumID, LastPostTitle = p.Title, LastPostAddedDate = p.AddedDate }).OrderBy(o=>o.ForumID) Currently the Join is not left join, meaning if some forum doesn't have a post that belongs to it, it will not be returned. The forum without posts must be returned with null (or default) values for the post properties. UPDATE The