linq

Counting records in C# using LINQ

I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as Units FROM tbl_Results GROUP BY ID, OptionName The results I got were: '1' '4' 'Approved' '40' '2' '1' 'Rejected' '19' '3' '2' 'Not Decided' '12' I have to encrypt the data in the database, and as such am unable to sum the data in relati...

Linq Query Performance , comparing Compiled query vs Non-Compiled.

Hello Guys, I was wondering if i extract the common where clause query into a common expression would it make my query much faster, if i have say something like 10 linq queries on a collection with exact same 1st part of the where clause. I have done a small example to explain a bit more . public class Person { public string Fir...

Settings variable values in a Moq Callback() call

I think I may be a bit confused on the syntax of the Moq Callback methods. When I try to do something like this: IFilter filter = new Filter(); List<IFoo> objects = new List<IFoo> { new Foo(), new Foo() }; IQueryable myFilteredFoos = null; mockObject.Setup(m => m.GetByFilter(It.IsAny<IFilter>())).Callback( (IFilter filter) => myFilte...

Entity Framework query not returning correctly enumerated results.

I have this really strange problem where my entity framework query isn't enumerating correctly. The SQL Server table I'm using has a table with a Sku field, and the column is "distinct". It isn't a key, but it doesn't contain any duplicate values. Using actual SQL with where, distinct and group by cluases I have confirmed this. Howev...

Instantiate object from Linq to XML Query

Hi All I have the following class public class CountrySpecificPIIEntity { public string Country { get; set; } public string CreditCardType { get; set; } public String Api { get; set; } public List<String> FilterList { get; set; } } I am trying to use a linq to XMl query to create a list of instances of type CountrySpecificPIIEn...

C# where does the dbml file come from?

Learning C# and learing Linq now. have lots of qestions about it. Basically I need a step by step tutorial. I suppose the dbml file is the configuration file of the database. I double click it and VS will open it with kind of design diagram. I can create/delete/modify table here? I can use add new item to add the Linq to SQL Classes to...

can LINQ being used with Sybase database?

Customer wants to move from SQL server to Sybase database. Don't know if LINQ can still be here? change is big? ...

Expression Tree with Property Inheritance causes an argument exception

Following this post: link text I'm trying to create an expression tree that references the property of a property. My code looks like this: public interface IFoo { void X {get;set;} } public interface IBar : IFoo { void Y {get;set;} } public interface IFooBarContainer { IBar Bar {get;set;} } public class Filterer { /...

Can someone describe the nested set model from a C#/LINQ perspective?

I know the nested set model doesn't pertain to the C# language or LINQ directly... it's what I'm using to develop my web app. For hierarchical data (categories with sub-categories in my case), I'm currently using something similar to the Adjacency List model. At the moment, I've only got 2 levels of categories, but I'd like to take it f...

How to specify dynamic field names in a Linq where clause?

If you create a Filter object that contains criteria for Linq that normally goes in a where clause like this: var myFilterObject = FilterFactory.GetBlank(); myFilterObject.AddCondition("Salary", "lessThan", "40000"); var myResult = myRepository.GetEmployees(myFilterObject); How would you match the Linq field to the Field Name with...

linq where clause and count result in null exception.

The code below works unless p.School.SchoolName turns out to be null, in which case it results in a NullReferenceException. if (ExistingUsers.Where(p => p.StudentID == item.StaffID && p.School.SchoolName == item.SchoolID).Count() > 0) { // Do stuff. } ExistingUsers is a list of users: public List<User> Exi...

How is this Nested Set SQL query converted into a LINQ query?

Querying a Nested Set Model table, here's the SQL... how can this be written in LINQ? SELECT parent.name FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = 'FLASH' ORDER BY parent.lft; particularly, the FROM part... never tried to do anything like that in LINQ. ...

C#, Linq, Dynamic Query: Code to filter a Dynamic query outside of the Repository

If you do something like this in your Repository: IQueryable<CarClass> GetCars(string condition, params object[] values) { return db.Cars.Where(condition, values); } And you set the condition and values outside of the repository: string condition = "CarMake == @Make"; object[] values = new string[] { Make = "Ford" }; var result ...

Linq join with an inner collection

Hi, I am trying a LINQ to Object query on 2 collections Customer.Orders Branches.Pending.Orders (Collection within a collection) I want to output each branch which is yet to deliver any order of the customer. var match = from order in customer.Orders join branch in Branches on order equals branch.Pending.Orders selec...

How to select a table from highest table use LinQ query

I have a sequence relationship: A has many Bs. B has many Cs. C has many Ds. They also make me so confused if there are more than 3 or 4,..tables. So, how can i select all Cs that satify A.Id="1". (something likes finding all grandsons of a grandfather) Thanks in advance. ...

What is LINQ? How can I use this with PHP?

Language Integrated Query. Now I know that the acronyms are. I have seen C# examples, but can't really understand them. Can I use them with PHP? Can I use it along with ORM? Has any PHP MVC framework has this? ...

Compiled query using list of class objects in C#

Hello , Can somebody help me out in creating compiled queries where input is to be a list of class objects? I have seen examples where Func<DataContext, somematchobject, IQueryable<T>> is created and compiled. But can I do something like Func<List<T>, matchObject, T>, and compile it? Basically I want an object(T) meeting certain conditi...

Mixing Silverlight-Specific System.Xml.Linq dll with Non-Silverlight System.Xml.Linq dll

I have a Logic layer that references Silverlight's System.Xml.Linq dll and a GUI that is in WPF (hence using the non-Silverlight System.Xml.Linq dll). When I attempt to pass an XElement from GUI project to a method in the Logic project, I am getting (basically) "XElement is not of type XElement" errors. To complicate matter, I am unable ...

How to ignore blank elements in linq query

How to ignore blank elements in linq query I have a linq query var usersInDatabase = from user in licenseUserTable where user.FirstName == first_name && user.LastName == last_name select user; But if I get here and first_name or last_name is blank then I want to still eval...

LINQ to SQL - How to efficiently do either an AND or an OR search for multiple criteria

I have an ASP.NET MVC site (which uses Linq To Sql for the ORM) and a situation where a client wants a search facility against a bespoke database whereby they can choose to either do an 'AND' search (all criteria match) or an 'OR' search (any criteria match). The query is quite complex and long and I want to know if there is a simple way...