linq

Selecting DataRows into new structures using LINQ. Calling Distinct() fails

Consider these two structures: struct Task { public Int32 Id; public String Name; public List<Registration> Registrations; } struct Registration { public Int32 Id; public Int32 TaskId; public String Comment; public Double Hours; } I am selecting a bunch of entries in a DataTable into new structures, like s...

Linq problem with group by clause

Hi all, I have some problems in executing a Linq query with the group by clause in a C# project. Here following the query: var items = ( from controlForm in dataContext.ControlForms join controlFormStatus in dataContext.ControlFormStatus on controlForm.FK_ControlFormStatus equals controlFormStatus.Id ...

Create LINQ query at runtime to GroupBy in EntityFramework (with inheritance).

This is the scenario. I have the following three classes, they are defined in Entity Framework, i only define them here for the example: public class Foo { public string Color { get; set; } } public class Bar : Foo { public string Height { get; set; } } public class Pipe : Foo { public string Width { get; set; } } So, I have m...

C# / LINQ / get all elements of sub-collection matching a condition ?

I have an : ObservableCollection<X> x_collection = new ObservableCollection(); public class X { public X() { Items = new ObservableCollection<Y>(); for(int i = 0; i < 10; i++) { Items.Add(new Y(i % 2 == 0)); } } public ObservableCollection<Y> Items {get; set;} } publi...

Order IList with field name in argument ...

Hello, I have this simple piece of code public ActionResult ListToGrid(string field, string direction) { _model.MyList = _repo.List(); } To sort, I can do this : _model.MyList = _employeeService.List().OrderBy(x => x.FirstName).ToList<Employee>(); But I'd like use "as field" the name receive (field) in argument and the directi...

Linq query with group by retrieving percentage

Hi all! I have to calculate the percentage in the group by clause. More in depth I need to calculate the percentage of ControlForms that have Status = 'False'. The Status is in the table checkResult. var items = (from controlForm in dataContext.ControlForms join controlFormStatus in dataContext.ControlFormStatus on controlF...

Randomize database table result with LINQ

from f in db.Table1 orderby Guid.NewGuid() select f this doesn't seem to work. how can i randomize results? ...

linq selecting problem (distinct)

List<NastavaIzvjestaj> nastava_izvjestaj = new List<NastavaIzvjestaj>(); var data_context = new DataEvidencijaDataContext(); int pomSum = 0; var prisustvo = (from j in data_context.nastava_prisustvos select j.br_indexa).Distinct(); var lista = prisustvo.ToList(); foreach ...

Force the Opening of the DataContext's Connection (LINQ)

Hi, When you create a datacontext, its connection is closed until you retrieve objects and it stays open when you retrieve objects in case you use deferred operators or late binding. Is it possible (in an extension method of the datacontext of not) to force the datacontext to open its Connection without querying LINQ with LINQ or doing...

source code for LINQ 101 samples

where can I find the code for GetCustomerList(), GetOrderList() for the LINQ 101 samples ...

LinqDataSource Select syntax

Why is it the syntax of the Select property in the LinqDataSource so different from Linq I would write inline in C#? I mean like: new (Id As MyId, Name As MyName) vs new (MyId = Id, MyName = Name) And the syntax diverges more when you start doing things like concatenation in the projection. I am using this with a Entity Data mode...

LINQPad - Log Secondary DataContext to "SQL" Tab

Its easy enough to instantiate multiple DataContexts in LINQPad. Is there any way to set these instances to Log to the "SQL" Tab of the results pane? Setting myDataContext.Log = this.Log doesn't work. ...

Get query syntax for a LINQ query

I have a linq query that i'd like to get the query syntax for. var q = customers.Where(x => x.name == "smith"); Is there something like IQueryable.ToQuerySyntaxString()? that would return something like this: from cust in customers where cust.name == "smith"; I'm asking because I can construct my query using method syntax, bu...

Is it possible to use linq in SQL Management Studio or use it in inline sql?

I would like to offload the work of generating an sql statement from the application to the database. Is this possible? ...

Converting an IEnumerable to a lookup with multiple keys per value

What's the best way to transform an IEnumerable into a lookup- or dictionary-like structure, but with multiple keys per value? What I'm looking for is something that does roughly the same thing as this, and in a generic way: var wordsByLetter = new Dictionary<char, HashSet<string>>(); foreach (string word in words) { foreach (char l...

Why does this NHibernate Linq query attempt to execute an insert statement?

I am encountering a very strange issue I wonder if anyone has seen before. I have as part of my Save() method in a repository that it will search out and find each associated tag by it's name. There is a line in there that looks like this. var tagRepo = (from t in tagRepository.Query() where t.Name == tag.Name select t).SingleOrDefault(...

Retrieve Pairs of Points from PointCollection?

I have a collection of Points, stored in a PointCollection. I need the points in the collection to draw lines. So, for example, if a point collection has four points, that will be two lines, as I use pairs of points in the collection to draw the line. I am looking for a way, preferably using linq, and as few lines of code as possible,...

How to write a LINQ to Entities query with List in a "WHERE" condition

I would like to know to to write most efficient LINQ (EDIT: to Entities) query with a list as a condition. Here is the thing. Lets say we have the following data structure: public class Recipe { public int Id; public string Name; public List<Ingredient> IngredientList; } public class Ingredient { public int Id; pub...

LINQ to XML - selecting XML to a strongly typed object

Hi I have a number of related issues but I will break the questions down into seperate posts. My XML is <Person>.....<Skills><Skill>Resus<Skill></Skills></Person> My code is : var products1 = from prd in xDoc.Descendants("Person") select new BusinessEntityLayer.Personnel { PayrollNo = (String)prd.Element("PayrollNumber"), FirstN...

Dealing with XElement null value

I have an xml that I am querying. One of the nodes is missing. So when I call XElement.Value I get a null exception. What is the best way to guard against this? I know I can write an extension method, but I am wondering is there something build into the language for this? ...