linq

LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'

Hi, I have two tables in my database - Category and Department which both contain the same columns - ID, Name and Code. I can create a new entity model using the Visual Studio 2008 designer and add the Department and it works fine - I can query the database using LINQ, alls good. When I update the model and add the Category table, th...

Linq no-noes - the catch all sql-like select?

Hi, One of the things that you have beaten into you as a junior developer is that you never, ever do a "SELECT *" on a data set, as it is unreliable for several reasons. Since moving over to Linq (firstly Linq to SQL and then the Entity Framework), I have wondered if the Linq equivalent is equally frowned upon? Eg var MyResult = fro...

Joins with multiple fields on GroupBy table data in LINQ query/method

Urgent: I have to work out how to write the following SQL query usingLINQ query or method syntax. (Edit: This is to return a list of latest AgentActivities for all Agents). Any help will be much appreciated. SELECT a.[AgentActivityId], a.[AgentId], a.[ActivityId], a.[StartedAt], a.[EndedAt], a.[Version] FROM [dbo].[AgentActivity...

Traversing a nested LINQ query in of anonymous objects in C# view via ASP.NET MVC

Traversing a nested LINQ query in of anonymous objects in C# view via ASP.NET MVC Hi, done a lot of searching and reading but still require some specific information. I have the following code: List<DateTime> range = getRangeCollection(); var range = ( from years in rangeData group years by ye...

Recursive Linq Function and Yielding

public static IEnumerable<UIElement> Traverse(this UIElementCollection source) { source.OfType<Grid>().SelectMany(v => Traverse(v.Children)); //This is the top level. foreach (UIElement item in source) { yield return item; } } This never returns anything recursively. I have b...

Linq to SQL query taking forever

Ok, first I thought I had a problem with how I was querying things. But apparently the problem lies in how linq translates my query to sql. Here's my linq: var items = (from p in ctx.bam_Prestatie_AllInstances join q in ctx.bam_Zending_AllRelationships on p.ActivityID equals q.ReferenceData join r in ctx.bam_Z...

FirstOrDefault() off of a LINQ versus FirstOrDefault() with a Lambda?

I am a bit curious as to which is considered "best practice" when it comes to FirstOrDefault. I've already seen this question, which is similar to the question I have, but not close enough that it answers my question. Which of these is "better code"? and why? var foos = GetMyEnumerableFoos(); var foo1 = (from f in foos where f.B...

How do I reference a field in Linq based on a dynamic fieldname

Firstly, apologies for the bad question title - not entirely sure if I am asking the correct thing. Normally I can do the following to access a field: MyTables table = dc.MyTables.SingleOrDefault(p => p.id == someId); somevalue = table.samplefield; In this instance the variable somevalue would end up having the value of the field sam...

Linq query issues - determining column at runtime

Hi Guys. I have a linq question (linq to sql). I have the following peice of code which works fine; var queryx = (from sa in d1.SampleAttributes where nodeTable.ToList().Distinct().Contains(sa.client_post_code_prefix) select sa.SampleId).Distinct(); Note: nodeTable is of type IQueryabl...

How do I specify the type of the range variable in a LINQ query?

How do I specify the type of the range variable in a linq query? ...

Complex Filtered Parent-Child LINQ to SQL Query

The title and tags of this post may be incorrect since my solution may end up being more MVC Controller - Model related instead of LINQ related... I am building an MVC (not that the platform is important) site to display Invoices and the Invoice Details for approval before being paid. Basically this gives me a standard Master-Detail dat...

Get raw list from OrderedEnumerable

I have an object which has a property that contains an IEnumerable of objects. Each object in the IEnumerable has a property that is also an IEnumerable. I am attempting to capture a larger list of the inner property. Unfortunately, the lambda expression that I am using is returning an OrderedEnumerable. I'd like to just retrieve a simpl...

Seemingly simple LINQ query is throwing me off. I want all items in array A unless it exists in array B

So I have this query. Both currentSurveyors and surveyorsInState are arrays of the same type. currentSurveyors could have 0 or more items. I want to return all the surveyorsInState except if that entry is in currentSurveyors. What am I doing wrong? from current in currentSurveyors from surveyors in surveyorsInState ...

How to query xsi:type from an attribute using Linq to XML?

Hello, Given this xml: <?xml version="1.0" encoding="utf-8"?> <EntityDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; <components> <component xsi:type="TypeA"> <Property1>100</Property1> </component> <component xsi:type="TypeB"> <Property2>100</...

How to make Linq extension methods available on ObservableCollection<T>

I've written a class that inherits from ObservableCollection<T>. I would like to be able to use the LINQ extensions methods like FirstOrDefault, like what you may get if you inherit from an implementation of IEnumerable<T>, or List<T>. How would I accomplish this? ...

why does my linq result show hex values in the debugger?

Hi I noticed when I look at say a result I got back from my database using linq to sql all fields like my primary key that is an auto increment field is shown as hex in my debugger. So like I will see in the debugger 0x00000006 instead of '6'. Is there any particular reason why? Kinda through me off guard when I wanted to take my resul...

Using Moq .GetMock to register ~1IRepository with Linq Expression?

Is it possible to Mock a Linq Expression via Moq using a Generic class such as ~1Repository. ~IRepository being something that is injected via an IoC such as StructureMap or Windsor? CODE TO TEST: var person = _repository.Find<Person>() .Where(p => p.Id == person.Id).SingleOrDefault(); TEST: var rep...

SQL Server Synonyms Performance with Views

If I have two databases and create a linked table (synonym) in one of them to the other one and then wrap that synonym in a view, will there be a performance issue? The reason I want to do this is to have SQLMetal see the synonym and generate a linq entity for it. The database are on the same server. Also if I did this 100+ times, all...

What is the equivalent of LINQ-to-SQL for Silverlight?

I have a WPF application that uses LINQ-to-SQL on a local .MDF file. This solution is simple, easy, and effective, i.e. I set up my model once, then read/write data anywhere via LINQ: using (var db = Datasource.GetContext()) { oldItem = (from i in db.Infos where i.Id == TheId ...

Group By either column using C# LINQ

I have a set of Data with columns such as below OffName,RO1,RO2,RO3 To explain further i use sample data as below: OffName RO1 RO2 RO3 A John Jack Rob B Earl John Carl C Rob Chris Kenny D Rodney Carl Jacob RO stands for Reporting Officer. Each Officer reports to upto 3 RO's.i need to make...