linq-to-entities

Entity Framework: combining exact and wildcard searching conditional on search term

删除回忆录丶 提交于 2019-12-12 21:19:50
问题 I'm creating a query to search the db using EF. TdsDb being the EF context. string searchValue = "Widget"; TdsDb tdsDb = new TdsDb(); IQueryable<Counterparty> counterparties; I can do exact match: counterparties = tdsDb.Counterparties.Where(x => x.CounterpartyName == searchValue); or wildcard match: counterparties = tdsDb.Counterparties.Where(x => x.CounterpartyName.Contains(searchValue)); But I want to be able to do both i.e. (psudo code) counterparties = tdsDb.Counterparties.Where(x => if

How to solve “The method 'Skip' is only supported for sorted input in LINQ to Entities.”

馋奶兔 提交于 2019-12-12 21:18:15
问题 I got this error when I was using "LINQ to entities" to show every single product and implement paging in ASP.NET MVC.: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'." LINQ: Model.Name = db.Products.Where(p => p.ProductSubcategoryID == id) .Skip((page - 1) * pageSize) .Take(pageSize) .ToList(); How can I fix it? What would happen if I put OrderBy instead of Where ? 回答1: You don't "put OrderBy instead of

Populate ComboBox based on another ComboBox using XAML

痴心易碎 提交于 2019-12-12 19:07:15
问题 I have two ComboBoxes <ComboBox Name="cmbMake" DisplayMemberPath="MakeName" SelectedValuePath="MakeID"/> <ComboBox Name="cmbModel" DisplayMemberPath="ModelName"/> I use LINQ-to-Entities to populate the cmbGroup ComboBox Dim db as myDataEntity cmbGroup.ItemsSource = db.Makes How do I populate my second ComboBox ( cmbModels ) based on the selection of the first ComboBox ( cmbMake ) using XAML so that whatever I select in the first ComboBox automatically filters the ItemsSource in the second

Reexecuting IQueryable on different DbContext instance?

♀尐吖头ヾ 提交于 2019-12-12 18:50:41
问题 I'm creating a repository layer to encapsulate DbContext . I would like to expose a fluent interface for building query, and dispose and create a new DbContext every time after a query request is sent: var repo = new EntityRepository(); repo = EntityRepository.FilterByAge(30).FilterByGender("Male"); var people = repo.GetPeople(); // GetPeople() should send the request, get the result, then dispose // the old DbContext and create a new one repo = repo.FilterByOccupation("Programmer"); var

linq query from two database

时光毁灭记忆、已成空白 提交于 2019-12-12 18:40:37
问题 I have a linq query from two database, however, each time the program stops at the query point. I don't know how to debug linq using VS. Can someone help me figure it out what's wrong here? Thank you. public List<Promotion> GetBroder(string source) { string _connString = ConfigurationManager.AppSettings["DB1"]; PromotionDataContext dc = new PromotionDataContext(_connString); string connString = ConfigurationManager.AppSettings["DB2"]; ReachDirectDataContext RDdc = new ReachDirectDataContext

How do I calculate a checksum on all columns in a row using LINQ and Entity Framework?

血红的双手。 提交于 2019-12-12 16:39:02
问题 The query I am trying to execute is similar to this: var checksum = from i in db.Items where i.Id == id select SqlFunctions.Checksum("*"); However, this returns the checksum value of the string "*" rather than evaluating the wildcard. Is there a way to calculate the checksum of all the columns instead? Update: var checksum = db.Database.SqlQuery<int?>("SELECT CHECKSUM(*) FROM [Catalog].[Item] WHERE Id = @p0", id); This gives me the result I want but seems dirty. Is there a way to do this

How to conditionally filter IQueryable by type using generic repository pattern

不问归期 提交于 2019-12-12 15:11:01
问题 I have a method in my generic repository that returns an IQueryable<T> : public virtual IQueryable<T> All { get { DbSet<T> set = Context.Set<T>(); if (typeof(T).IsSubclassOf(typeof(OrganisationDependent))) return set.AsEnumerable() .Cast<OrganisationDependent>() .Where(x => x.OrganisationID == CurrentOrganisationID) .AsQueryable() .Cast<T>(); return set; } } The reason for the if statement is that most, but not all of my tables have an OrganisationID and I want to ensure that a user only sees

DbExpressionBinding requires an input expression with a collection ResultType

别等时光非礼了梦想. 提交于 2019-12-12 15:10:13
问题 I want to group the table by a column and get the counts, then create dictionary using the result. The last statement returns the error of DbExpressionBinding requires an input expression with a collection ResultType. What the error means? var a = context.Table; var b = a.GroupBy(x => x.RecordType, (k, cnt) => new { RecType = k, cnt = k.Count() }); var c = b.Select(x => new KeyValuePair<string, Tuple<Type, int>>( x.RecType, Tuple.Create(ObjTypes[x.RecType], x.cnt))) .ToDictionary(x => x.Key,

How do I use LINQ to Entities in Visual Basic?

别说谁变了你拦得住时间么 提交于 2019-12-12 14:11:37
问题 I've created a .NET solution with two projects: ToyData (Visual Basic Class Library) ToyOne (Visual Basic WPF Application) The ToyData project contains Toy.edmx , an ADO.NET Entity Data Model generated from a database called Toy. The ToyOne project contains this Window1.xaml.vb file: 1 Imports ToyData 2 3 Class Window1 4 5 Private Sub Window1_Loaded( _ 6 ByVal sender As System.Object, _ 7 ByVal e As System.Windows.RoutedEventArgs) _ 8 Handles MyBase.Loaded 9 10 Dim dc As New ToyEntities 11

Unable to create a constant value - only primitive types or Enumeration types allowed

陌路散爱 提交于 2019-12-12 13:21:16
问题 I have seen some questions related to this Exception here but none made me understand the root cause of the problem. So here we have one more... var testquery = ((from le in context.LoanEMIs.Include("LoanPmnt") join lp in context.LoanPmnts on le.Id equals lp.LoanEMIId where lp.PmntDtTm < date && lp.IsPaid == false && le.IsActive == true && lp.Amount > 0 select new ObjGetAllPendingPmntDetails { Id = lp.Id, Table = "LoanEMI", loanEMIId = lp.LoanEMIId, Name = le.AcHead, Ref = SqlFunctions