Questions tagged [linq]

43375 questions
1

votes
0

answer
150

Views

Performance Improvement with Entity Framework 6 compiled queries

From the Compiled Queries documentation: Starting with the .NET Framework 4.5, LINQ queries are cached automatically. However, you can still use compiled LINQ queries to reduce this cost in later executions and compiled queries can be more efficient than LINQ queries that are automatically cached. N...
user3723486
1

votes
0

answer
129

Views

How to perform a LINQ join on IQueryable<T> in odata controller

I have a WebAPI method which uses the OData Query Options to return data to the client. The entity has cityid and I want cityname from another entity using joins. I have tried using below Api method, which is incomplete. Entity1:- public partial class UU_DeliveryCharges { public int DeliveryChargeId...
Sunil Chaudhary
1

votes
2

answer
66

Views

Finding strings using LINQ [duplicate]

This question already has an answer here: Why writing items to console writes only namespace and class name instead of data? [duplicate] 5 answers After some research into C# and some test with LINQ with easy examples. I wanted to apply this knowledge into my problem. My DataStructure as seen from...
WhiteGlove
1

votes
1

answer
50

Views

LINQ to SQL query succeeds or fails based on using foreign key versus using a built in navigation property

I have a query against a SQL server database that throws an Error converting data type nvarchar to numeric. exception. I was trying to use Convert.ToDecimal on a varchar field, however I'll eyeballed the data as best I could and couldn't find an invalid value. The query is filtering the table by a...
Terry
1

votes
3

answer
257

Views

Linq query : select content of an ID when max(date)

Happy new Year everyone ! :-) I've searched for several hours but I can't figure out how to build my query properly using Linq. I have 2 tables : A and B In table A : I have A_ID, B_ID, VALUE_DATE. In table B : I have B_ID, STATUS. Example : A_ID = 547 VALUE_DATE = 01/05/10 B_ID = 14750 A_ID = 547...
Florian
1

votes
0

answer
55

Views

Autocomplete performance issue

I am using textbox autocomplete features with large data. Instead of hitting SQL Server every time, I keep all data in an application variable and then with the help of Linq and C#, I am fetching the data, but performance is not good - sometimes retrial was late and after tabbing data list getting f...
Amol Naik
1

votes
1

answer
221

Views

Linqpad - Linq to Sql Ambiguous table and linked server name

Using Linqpad & Linq to Sql, I am trying to join some data from a table in the database to data that which is contained on a linked server. I have followed the instructions to create a connection with the linked server (http://www.linqpad.net/FAQ.aspx#cross-database) My problem seems to be that I ha...
Mike B
1

votes
0

answer
114

Views

LINQ Where causing 'Arithmetic overflow error for data type smallint, value = 65676'

So I recently update my entity model from the db to reflect some changes to various columns. I believe the only changes were making some 'smallint' into regular int. Now when I run these following lines, I get the 'Arithmetic overflow error for data type smallint, value = 65676' error, but the probl...
Ivan
1

votes
1

answer
110

Views

LINQ query to find related data [duplicate]

This question already has an answer here: Projecting self referencing multi level Entities In Entity Framework 6 2 answers I got some help with my recursive product category tree view here on Stack Overflow before, and this is working: Entity model: public class ProductCategory { public int Id { g...
Stian
1

votes
1

answer
148

Views

Multiple Grouping with IQueryable and returning a list of objects in C# linq query

I have a table ot_request like in this link. I have to return a list of ot_request entity class with the following conditions. I have to filter the list where the status_id==5 After the filtering, I have to group the rows by their project_id After the grouping by project_id, there will be some creat...
Abdullah
1

votes
1

answer
27

Views

How can I update my List loop in my DataGridView?

I have a list attached to the datasource of a gridview, when I make the change from my list in the status field I would like to change the status in gridview as well. My loop is through the datagridview and it is linked in my list, I think the problem is this? How can I update my List loop in my Dat...
Will
1

votes
2

answer
37

Views

XML & Lambda - accessing fields that don't implement the IEnumerable interface

I'm using Linq/Lambda to write output to an XML file, just in the standard way: new XElement('Employees', from emp in empList select new XElement('Employee', new XAttribute('ID', emp.ID), new XElement('FName', emp.FName), new XElement('LName', emp.LName), new XElement('DOB', emp.DOB), new XElement('...
user5956088
1

votes
1

answer
69

Views

C# GroupBy in query

I have 3 data sets. One for cars (Auto) which is attached to a brand & model (AutoType) and an Owner (Eigenaar) public class Auto : BaseDbEntity { public string Kleur { get; set; } public DateTime DatumGekocht { get; set; } public string Nummerplaat { get; set; } //relations public Eigenaar HuidigeE...
Robbe R
1

votes
0

answer
628

Views

System.Data.DataException: An exception occured while initializing the database

Please help me on this. I have written a code for getting the first record from the database using LINQ My Homecontroler [CustomHandleError] public class HomeController : Controller { Domain domain; public HomeController() { domain = new Domain(); } public ActionResult Index() { HomePageViewModel vm...
Reshmy
1

votes
0

answer
222

Views

Left join big DataTables in C#

I have 3 big tables in SQL Server: Table1 (ID1, ..., ID2) => around 3 million rows Table2 (..., ID1, ...) => around 50000 rows Table3 (..., ID2, ...) => around 8 million rows The first table is the 'main' table, the second table has the foreign key to the first one (ID1), and the third table has a f...
HermioneLumus
1

votes
2

answer
81

Views

How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?

I am new to ASP.NET MVC. I was trying to build a model as follows (assume the syntax is alright). I wanted to know how to bind and save an object that contains list of objects. For eg. in the following code, I want to save an object of type SchoolGrade to the database (it works fine if it does not c...
rakshithnrr
1

votes
1

answer
42

Views

Sending an Entity on email using Mailer ASP.net

Trying to develop an app where when a User applies for an appointment, they get email confirmation that the appointment has been received with their details in the email. I've got a linq query( after the appointment is created) to get their details from the database. I've debugged the app, and the d...
Anna Doe
1

votes
2

answer
87

Views

Adding count from another table to variable with method chain

I have a table of posts, and a table of likes. All likes for a post id have to be counted in order to get the total likes for each post. Currently I do this by looping through all the posts which is very inefficient, but I cannot find the correct way to do this in a single database call. List posts...
Maflores16
1

votes
0

answer
76

Views

Filtering included object with OfType<T> for specific ThenInclude by type T

Considering the classes : public class MainObject { List myList; } public class Base { int MainObjectId; int x; } public class A : Base { int y; } public class B : Base { int z; AnotherObject anotherObject; } With such a configuration i want to include different things depending on the type of the e...
G.Dealmeida
1

votes
0

answer
170

Views

EF Core Include with FromSql extension without sub SELECT

I have some query to get companies from database like this: var fullTextSearchVal = 'test'; var sql = $'SELECT * FROM Companies WHERE CONTAINS(*, '\'*{fullTextSearchVal}*\'')'; var result = _context.Companies.Include(i => i.Address).FromSql(sql).ToList(); It works well, but have a little problem aft...
1

votes
1

answer
46

Views

generated SQL Query changes based on different selection in Linq

I am converting an SQL query to LINQ. It has multiple inner, left and right joins. I'm checking the generated SQL from LINQ in every step. But the problem is based on the selection the generated sql query changes. Below my Linq var a = from freight in billingEntity.FreightCharges.AsNoTracking() join...
Riki
1

votes
1

answer
55

Views

LINQ ThenBy in circle

Why I have different results in gg? Version: var kk = ff.OrderBy(el => el.path[0]); for (var i = 1; i el.path.Length > i ? el.path[i] : 0); } var gg = kk.ToList(); Version: var kk = ff.OrderBy(el => el.path[0]); kk = kk .ThenBy(el => el.path.Length > 1 ? el.path[1] : 0) .ThenBy(el => el.path.Length...
Kakao Developer
1

votes
1

answer
278

Views

Parse string using C# LINQ

Need help in parse string using C# LINQ I have a string like this 11=205129022,453=8,448=CompanyID,447=D,452=63,448=userid,447=D,452=11,448=CompanyName,447=D,452=13,448=W,447=D,452=54,77=O,555=2 I would like to split this string on 448= then take an array start getting line 'starts with 448' get the...
Moh Mah
1

votes
1

answer
260

Views

LINQ with group by for nested list

I am trying to filter and get a list if my main object using Linq with my nested lists. what I require is - List class Category contains List and List But the result i get is grouped by both the child objects, hence i dont get the exact result. I want to group this seperatly to get the result. Below...
Praddy
1

votes
2

answer
99

Views

adding a new element below specific node in a xml file using LINQ - C#

I'm new in C# and I have tried everything during days and days but I have no answers to solve my problem. I have a xml doc like this that populate a treeview in a windows form app: I have to include a new element below the node with title 'Type 2' or other specified node. I'm using Linq in my applic...
Fabricio Leinat
1

votes
0

answer
207

Views

Error when filtering on an Include in Entity Framework Core

I have the following EF Core linq statement: var contact = dataContext.Contacts .Include(c => c.Addresses) .ThenInclude(a => a.AddressType) .Where(c => c.ContactId == contactId) .Select(c => new Contact() { ContactId = c.ContactId, Title = c.Title, FirstName = c.FirstName, Surname = c.Surname, Addre...
Carl Rippon
1

votes
0

answer
309

Views

Change name of properties saved into Cosmos Db without using JsonProperty attribute

I need to change the name of the properties saved into Cosmos Db. I don't want to use the JsonProperty attribute from Newtonsoft.Json because using it has the effect to always change the property's names, also in JSON response when I call an action method. So I would that only the JSON saved into Co...
Matteo
1

votes
0

answer
57

Views

Expression type not supported using sum in Nhibernate

I am trying to use Sum in NHibernate and I am getting this error. Expression type 'NhSumExpression' is not supported by this SelectClauseVisitor. Below is my query. AmountShort = _session.Query().Where(conditions).Select(x => x.AmountShort ?? 0).Sum() I have also tried this AmountShort = _session.Q...
Testing123
1

votes
1

answer
78

Views

convert SQL to LINQ not working

I want to convert this SQL code to LINQ. Here is my SQL code: SELECT Rooms.RoomName AS RoomNo, Beds.BedName AS Beds, Rooms.RoomType, ISNULL(CheckIn.CheckIntatus,'') AS Status FROM CheckIn INNER JOIN GuestBeds ON CheckIn.GuestBedId = GuestBeds.Id AND (CheckIn.CheckInStatus = 1 OR CheckIn.CheckIntatu...
yusry
1

votes
3

answer
1.2k

Views

Changing DateTime format in lambda Expression

I have a DTO field in DateTime format public DateTime date_joined { get; set; } I use this to turn the data into a Json public JsonResult Customer(int? id) { var user = User.Identity.Name; if (!AccountController.IsInRole(user, 'admin')) { return null; } id = id == null ? 0 : id; var customer = db.Pr...
Ege Bayrak
1

votes
1

answer
50

Views

LinqToSql - Extension Method Resolution

I understand the preference is given to instance method over extension method during compile time resolution. However for LinqToSQL (or for LinqtoXXX) : Table orders = context.Orders var query = orders.Where(o=>o.name=='xyz') What is deciding factor for resolving above Where method of Queryable clas...
Rahul Agarwal
1

votes
1

answer
131

Views

Json.NET LINQ query for single JProperty on JArray of JObjects

I have a JArray with this configuration [ { 'name' : 'text', 'age' : 32 }, { 'name' : 'text2', 'age' : 33 }, ] and I want to use LINQ query to select a JArray containing JObjects with just the key value of a specified given key. For example: GetCollectionOfPropertiesByKey('name'); this would result...
xDGameStudios
1

votes
0

answer
189

Views

XDocument asynchronous loading options

dotnetcore 2.0 provides these methods for asynchronous loading of XDocument: public static Task LoadAsync(Stream stream, LoadOptions options, CancellationToken cancellationToken); public static Task LoadAsync(TextReader textReader, LoadOptions options, CancellationToken cancellationToken); public st...
redcalx
1

votes
3

answer
82

Views

Split list into sublist ID wise C#

Suppose, I have a list of Journals: class Journal { public int ID; public string Name; } var objListJournals = new List { new Journal{ID = 1, Name = 'Tom'}, new Journal{ID = 2, Name = 'Jerry'}, new Journal{ID = 1, Name = 'Brad'}, new Journal{ID = 3, Name = 'Frog'}, new Journal{ID = 1, Name = 'Alex...
Hasin Mahjabeen
0

votes
0

answer
4

Views

Linq with multiple foreach inserting items to table

I have list of ids like this below List ids = new List(); and then i have list of lengths which is also integers like this below.. List lengths = new List(); now i need to insert into table using linq query with the data format like this below ID length 1 1 1 2 1 3 2 1 2 2 2 3 f...
Enigma State
0

votes
0

answer
6

Views

MVC Error - Requires a model item of type IEnumerable

I'm new to both MVC and LinqToSql. I'm trying to create a small application that lists contacts using both technologies. My Model: public class Contact { [Key] public int Id { get; set; } [Required] public string Name { get; set; } [Range(18, 99)] public int? Age { get; set; } [EmailAddress] public...
Jadenkun
1

votes
1

answer
185

Views

LINQ - Creating Subquery based on Where clause

I'm seeing behavior in LINQ that I just don't understand and I really want to prevent it from happening because it's causing my queries to run much slower than they should. The following C# code produces the results I'm expecting but when I add an additional dynamic filter to it things start to go s...
spyder1329
1

votes
0

answer
72

Views

LINQ - EXISTS, Reference Parent Field in Subquery

I'm working on creating a dynamic linq expression with an optional where clause containing 'exists'. Almost everything about this can be dynamic. The user can choose to filter only root level nodes or filter both the root and all descendant nodes. I've mocked up a working example of one of the pat...
spyder1329
1

votes
1

answer
56

Views

c# Remove nodes from xml file irrespective of hierarchy

Please see my demo. I want to remove all the duplicate placemark nodes irrespective of the hierarchy. Is this possible with Linq? What would be the best way to remove all duplicate nodes and save as new xml file. Also please note that for demo I have created a string otherwise I would be loading the...
user1254053
1

votes
2

answer
282

Views

LINQPAD - PredicateBuilder throwing error

When I run the statement below in Linqpad it's throwing the following error: The LINQ expression node type 'Invoke' is not supported in LINQ to Entities. This seems to match some of the other examples I've seen online. What am I doing wrong? LinqPad version 5 Language: C# Statement Connection: C# p...
spyder1329

View additional questions