linq-to-entities

Error in Linq: The text data type cannot be selected as DISTINCT because it is not comparable

六眼飞鱼酱① 提交于 2019-12-12 04:08:29
问题 I've a problem with LINQ. Basically a third party database that I need to connect to is using the now depreciated text field (I can't change this) and I need to execute a distinct clause in my linq on results that contain this field. I don't want to do a ToList() before executing the Distinct() as that will result in thousands of records coming back from the database that I don't require and will annoy the client as they get charged for bandwidth usage. I only need the first 15 distinct

Linq woes - A CollectionType is required

↘锁芯ラ 提交于 2019-12-12 03:47:24
问题 Why doesn't my linq query work? I suspect it may have something to do with lazy loading. It seems to work in linqpad. public IList<PaymentDto> GetDocumentPayments(int documentId, bool? allowRepeatPayments, byte[] paymentStatuses, int[] paymentMethods) { using (var ctx = ObjectContextManager<MyDataContext>.GetManager("MyDataContext")) { var payments = new List<PaymentDto>(); var ps = new List<byte>(); if (paymentStatuses != null) { ps = paymentStatuses.ToList(); } var pm = new List<int>(); if

How to Call SQLite User Defined Function with C# LINQ Query

只谈情不闲聊 提交于 2019-12-12 03:44:35
问题 With SQLite and C#, has anyone tried calling a UDF within a LINQ query? Searching online, I found this about creating a UDF function in C# http://www.ivankristianto.com/howto-make-user-defined-function-in-sqlite-ado-net-with-csharp/ As for calling a function in LINQ to Entities, I have the solution here Calling DB Function with Entity Framework 6 Here's what I got so far. I create my database model and linq to SQLite. I add this into the database model file: <Function Name="fn

how to store dynamically the desired value in my linqued query variable

旧巷老猫 提交于 2019-12-12 03:27:31
问题 I have this query var empno = (from LApp in db.Employees select new { LApp.EmployeeNumber }).ToList(); when executed I have this in empno what I want is to trim the starting zeros and store it in empno i.e {EmployeeNumber="2"} and so on... How is it possible? 回答1: You can use TrimStart() to remove leading zeros, something like this : var empno = (from LApp in db.Employees select new { EmployeeNumber = LApp.EmployeeNumber.TrimStart('0') } ).ToList(); I have no idea if TrimStart() can be

LINQ request with 2 related entities

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 03:19:31
问题 I have the following code: var result = from i in _dbContext.Users orderby i.aspnet_User.aspnet_Membership.CreateDate select new ITW2012Mobile.Models.BusinessLogic.PreReg() { UserID = i.ID, Company = i.Company, FirstName = i.FirstName, LastName = i.LastName, IsUsed = i.IsUsed, Title = i.Title, Email = ((i.IsUsed) ? i.aspnet_User.aspnet_Membership.Email : i.PreRegEmail.Email) }; var q = result.ToList(); so, when flag isUsed == true we need get email from membership, otherwise from PreRegEmail.

Only parameterless constructors and initializers are supported in LINQ to Entities?

笑着哭i 提交于 2019-12-12 03:08:55
问题 I have a ProductViewModel that converts a DTO (Product) at runtime. public class ProductViewModel : IViewModel { public ProductViewModel() { Categories = new List<CategoryViewModel>(); } #region DTO Helpers public ProductViewModel(Product p) { this.ID = p.ID; this.Name = p.Name; this.Price = p.Price; Categories = new List<CategoryViewModel>(); } #endregion public int ID { get; set; } public string Name { get; set; } public decimal Price { get; set; } public IEnumerable<CategoryViewModel>

Dynamic predicate building in EF5

拜拜、爱过 提交于 2019-12-12 03:08:48
问题 Ok, I have to build a predicate to select an unknown number of columns from a known entity in EF5 AND filter by an unknown number of columns ONE of which will always be a name from a child collection. So this is what I need to end up with var q = db.Set<Entity>() .Where(e => e.Code.Contains("q") || e.Translations.FirstOrDefault(t => t.Culture.ID == "whatever").Description.Contains("q")) .Select(e => new { e.ID, e.Code, Name = e.Translations.FirstOrDefault(t => t.Culture.ID == "whatever")

Why is there no int overload for SqlFunctions.StringConvert

时间秒杀一切 提交于 2019-12-12 03:08:31
问题 Title pretty much sums it up. I'd bet that over half the usage of this method in the wild involves someone casting an int to a decimal or double in order to use it, so why isn't there just an overload that takes an int as the argument? http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.stringconvert.aspx 回答1: Because it's directly related to the STR function found here: http://msdn.microsoft.com/en-us/library/ms189527.aspx and that function only takes a float,

LINQ to Entities does not recognize the method

笑着哭i 提交于 2019-12-12 02:36:44
问题 LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method, and this method cannot be translated into a store expression. var warranty_yes = from i in devicesEntities.device where i.WarrantyDate >= DateTime.Now select i.Id; var warranty_yes_list = warranty_yes.ToList(); var view_query = from i in devprim_by_status where warranty_yes_list.Contains(i.Id) select i; which is a solution for this problem? 回答1: You can do it in a single query: var view_query = from i in devprim

Efficient way to query each element of a list

南楼画角 提交于 2019-12-12 02:33:56
问题 I have to iterate through a collection of objects (let's say ID's), and execute a specific query for each of these objects. For example: IEnumerable<int> ids = getIDs(); //[1,2,3,4...] Right now I have this solution: DBEntities db = new DBEntities(); var results = from a in db.TABLEA join b in db.TABLEB on a.id equals b.id join c in db.TABLEC on b.oid equals c.oid where ids.Contains(c.id) select a; but keep in mind that the list of IDs is smaller than the table where I am searching. That