linq

Casting Linq Query to a Structure in .Net

Good Morning, I have created a structure to be a cut down version of a Class that I already have. I am trying to use a linq query to iterate through the list of MyClass and create a List based on the results (A discinct list based on a few of the properties on MyClass). Something like this... List<MyStructure> thisList = (from MyClas...

How do I retrieve only certain subobjects in LINQ?

I have an entity object (Company) which has 1 or more subobjects (CompanyRevision) represented as a non-null FK relationship in the database. Using LINQ, I want to get all the Companies from the database, but I also only want the latest CompanyRevision for each company. This is how I do it today, but I have a feeling this could be done u...

Using max and join together in linq using c#

I have a master-detail tables and I want to get list of master join with detial where detail is max in some filed. for example I have a table named Document and also a child table named Revision .I want to get list of document join Revision where Revision filed is max ? One solution is: using ( ProcurementDataContext dc = new Procurem...

unable to edit DataGridView populated with results of LINQ query

When i use the results of a linq-to-xml query to populate a datagridview, i cannot edit the datagridview. i've tried setting the datagridview's readonly property to false and that doesn't help. I also added an event handler for cellBeginEdit and put a breakpoint there, but it doesn't get hit. Any idea what i'm doing wrong, or if this ...

How do I project a new type from a hierachical data structure using linq

We've got a Class called Group, it contains a collection of questions and it also contains collection of groups (i.e. we have a potential nesting of groups). Expressed in XML this might look something like this: <group id="Group1"> <questions> <question id="questions11"/> </questions> <groups> <group id="group12"> ...

Linq to entities multiple WHERE clause &&

In the code bdehind && syntax does not work, any ideas? Entities ctx3 = new Entities(); var uniqueQuote = from quot in ctx3.Quotes.Include("aspnet_Users").Include("Enquiries") where quot.Enquiries.EnquiryId == selectedEnquiryId && quot.aspnet_Users.UserId == currentUserId orderby quot.QuotePrice select quot; Error 2 Del...

LINQ Dynamic Where - string parameter missing

Hello, I am sure this is something really simple, but I am going bonkers trying to figure out why. I have the following code: string condition = string.Format("{0}.Contains({1})", column, value); var query = DataContext.MyTable.Where(condition); The strange thing that I am getting is an error saying: Argument cannot convert from s...

is this LINQ save function acceptable? (to have a shared INSERT/UPDATE logic like this?) looking for feedback...

Hey guys, new to WPF and really struggling with 'the right way' to do things... public void Save(CompanyContact entityToSave) { try { var saveEntity = (from cc in db.CompanyContacts where cc.CompanyContactId == entityToSave.CompanyContactId select cc).SingleOrDefault(); if (saveEntity == null) { /...

better EdmGen2?

Hi, I'm using the microsoft EdmGen2 to generate my edmx file from my database. The tool works great, but there is one things missing - it doesn't support function. Is there a way to map all the stored procedures to be functions? Also, if i'm editing it manually, the next time im running this tool, i'm losing all those changes. my ques...

Check if values of Dictionary contains an element with certain field value

I have a Dictionary private readonly Dictionary<int, BinaryAssetExtensionDto> _identityMap; And I would like to do something like this: if(_identityMap.Values.Contains(x => x.extension == extension))... Is this possible because previous code doesn't work. Now I'm doing it like this: var result = _identityMap.Values.ToList().Find(...

Help converting an sql query into LINQ

Hey, I'm trying to convert some SQL I've patched together into a LINQ query. The context is a simple quiz app. A quiz has quizrounds, contestants are one to one attached to quizrounds, rounds have registered answers. The case is that I have a one to many relationship between tables 'quizround' and 'answer'. I want to do a ranking of a...

Linq to entities : Stored procedure + set navigation properties

Hello, I set an object entity with a stored procedure, but the navigation properties are always equals at null ... The aim is to include or join an external entity. Any idea ? Thanks ...

Linq macro substitution

Hi I can build up a sql string by concatenating a string i.e. String fieldname =”Company”; String tableName= “Address”; “select “+fieldname+” from “+tableName How can I do this with Linq? TIA Stuart ...

entity framework nested conditions

Hi i have a model where i have to count the rows with conditions: uscitaservizio == false and accountid attribute (int) is in a string filtrodipe es: filtrodipe = "2,4,5,6" I' ve tried with this: db.TDP_Missioni.Count( p => p.UscitaServizio == false && (objUser.FiltroDipe != null ? (p.AccountID.ToString() in objU...

Multiple layers of foreign keys in LINQ query

If I have Table3 that has a FK pointing to Table2 which has a FK pointing to Table1 what I'm seeing via intellisense is I can reference Table2 from Table3 but I can't go Table3.Table2.Table1 it only goes one layer deep. from t3 in Table3 where t3.t2.property == "some value" && t3.t2.t1.property == "some other value" select t3.t2.t1; T...

Methode Call twice inside LINQ Query

In our application we have a little query that looks like this: var selectedAgents = from agent in listAgents where (this.collectionVehicles.GetVehicleByAgent(agent)).IsActive || (this.collectionVehicles.GetVehicleByAgent(agent)).IsSoldSinceCurrentSession select agent; This works fine but the method GetVehic...

Multiple Order By with LINQ

I start with a basic class that I want to manipulate in a List using LINQ, something like the following: public class FooBar { public virtual int Id { get; set; } public virtual string Foo{ get; set; } public virtual string Bar{ get; set; } } This is what I ultimately found out to solve my problem using the non ...

What's the best LINQ way to filter a sequence around a pivot?

I'm playing with QuickSort and LINQ, and want to separate the sequence into item before, equal-to, and after a pivot. Here's what I have so far: public static Tuple<IEnumerable<T>, IEnumerable<T>, IEnumerable<T>> ComparativeWhere<T>(this IEnumerable<T> source, T t) where T : IComparable<T> { return new Tuple<IEn...

Return different objects with one LINQtoSQL statement

Here's my problem: I have an IPerson which is implemented by Employee and Student. What I really want is what you see below. One LINQ statement to get each type of IPerson. This works great until I call the method ;). It makes sense as to why I'd get the error, but I am really struggling as to find a decent way to pull all IPerson o...

linq query where column contains null values

I have the following query Dim get_rmf_2 = From rmf In t_rmf _ Where rmf!NIVP = nivp_rap When i run it i get an error : Operator '=' is not defined for type 'DBNull' and string "test". I suspect this is because the column "NIVP" in the datatable contains null values, I've tried yhe same thing without null values and it works...