linq

LINQ Provider to RESTful service

I'm writing a custom LINQ provider to a RESTful service. Some of the calls in the service return summary headers, including total number of records, pagesize, and the page of the return. I need clean a way to return this data. Given this query: var foo = from x in ctx.MyQueryableThingie select x; The "foo" variable above b...

For Linq Select Statement, how to construct a SubClass in a general way at run time?

The fake code is: public class ParentClass { decimal quantity, decimal price } IQueryable<ParentClass> parents; //SonClass is created at run time //SonClass has some extra property, such as amount=quantity*price, which is defined by a string at run time IQueryable<SonClass> sons=parents.Select(p=>new S...

why does C# convertall require 2 parameters

I have an array of Car objects I want to conver them to a list of Vehicle objects I thought this would work Vehicle[] vehicles = cars.ConvertAll(car=> ConvertToVehicle(car)).ToArray(); but its complaining that ConvertAll requires two parameters. here is the error: Error 2 Using the generic method 'System.Array.ConvertAll(TInput[...

How can i do insert more data with insert query with more select statment linqToSQL?

How can i use insert into myTABLE (. . . .. . ) select * from(.......). I try to write some linq query but i can not. Look please linq query. SQL: INSERT INTO ActualAmountsByLocation (ItemBarcode, Location, LocationName, Amount, isCustomerItem, LastUpdate) SELECT * FROM (SELECT DISTINCT m.ItemBarcode, ...

Language Integrated Query.

What is the purpose of LINQ? Where LINQ can be used? How necessary is it to learn the LINQ? What is the best way to learn LINQ? ...

C# LINQ/Lambda scope question

I get the error : A local variable named 's' cannot be declared in this scope because it would give a different meaning to 's', which is already used in a 'child' scope to denote something else. static void Main(string[] args) { string s = "hello"; // Line 1 var test = new[] { "abd", "def" }.Select(s => s.StartsWith...

How can i use in ( select method in linq?

how can i write "in( select" method in linq? I'm trying to convert : UPDATE ActualAmountsByLocation SET isCustomerItem=1 WHERE ItemBarcode IN (SELECT barcode FROM StockMaterials WHERE barcode=@Barcode AND ownership=1) I've tried like this: Array stockMaterials = ( from s in stockMovementCtx.StockMaterials where s.barcode == Barc...

Problem with a method that accepts 2 lambdas

I have the following class: public class MyClass<T> where T : class { public void Method1<TResult>(T obj, Expression<Func<T, TResult>> expression) { //Do some work here... } public void Method2<TResult>(T obj, Expression<Func<T, TResult>> expression1, Expression<Func<T, TResult>>...

Linq2SQL "Local sequence cannot be used in LINQ to SQL" error

I have a piece of code which combines an in-memory list with some data held in a database. This works just fine in my unit tests (using a mocked Linq2SqlRepository which uses List). public IRepository<OrderItem> orderItems { get; set; } private List<OrderHeld> _releasedOrders = null; private List<OrderHeld> releasedOrders ...

Sum properties using LINQ

DealsThisMonthOpen = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Open").Count(), DealsThisMonthLost = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Lost").Count(), DealsThisMonthWon = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Won")...

Is it possible to re-group the result of using a LINQ GroupBy()?

If I have used LINQ's GroupBy() method to create a grouped enumeration, is it possible to regroup that result under another key system? That is, if I grouped all of the objects by property X for one part of the code, is it possible to subsequently group that collection by property Y of property X at a later point in the code? ...

Union-ing two custom classes returns duplicates

I have two custom classes, ChangeRequest and ChangeRequests, where a ChangeRequests can contain many ChangeRequest instances. public class ChangeRequests : IXmlSerializable, ICloneable, IEnumerable<ChangeRequest>, IEquatable<ChangeRequests> { ... } public class ChangeRequest : ICloneable, IXmlSerializable, IEquatable<ChangeRequest>...

Alternative to using string.Join in a LINQ query?

Any time I use the code below it throws the error: Method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' has no supported translation to SQL. var items = from t in fc.table where t.id== objId select new { ...

Can't get Database to update using Repository Pattern and LINQ to SQL.

My project has the following setup Controller -> Service (for validation) -> Repository (for LINQ) -> dbml -> Database Controller ''# <AcceptVerbs(HttpVerbs.Post)> - hiding this line so that code formatting looks proper in SO. Function Edit(ByVal user As Domain.User, ByVal id As Integer) As ActionResult If ModelState.IsVali...

LINQ query for full text searching

How can I query a collection for a keyword like "John Doe" where the value of a property might be "John M Doe"? Doing a contains certainly will not work but below is an idea of what I'm after. people, for reference, is a List containing Person objects that have Name and Description properties. string keyword = "John Doe"; var q = from...

Use LINQ to find complex combinations of items in two lists

This question is very similar to a previous question of mine, Use LINQ to count the number of combinations existing in two lists, except with some further twists. I have a list of CartItems that can receive a discount based on the items specified in the list of DiscountItems. I need to be able to pull out the items in the Cart that c...

loop one sequence and check againt other list

Hi, i have a namespace string like "Company.Product.Sub1.Sub2.IService". The Sub1/Sub2 can differ in their count, but normally their is one part which matches to a Dictionary with AssemblyFullname as key and path to it as value. Now ive written this code string fullName = interfaceCodeElement.FullName; var fullNamePart...

Getting Only The Top Row From Each Group

I have some code that groups a table by "Value1" and some loops that add the top row of each group to a list. This is a pretty ugly way to do this, and I was wondering if I could replace one of the foreach loops with a couple more lines in my LINQ query? Problem is, I don't have the foggiest idea how to do this. var Result = from a in...

Traversing complex XML document in C# with XLinq

So I want to access the child element in a structure that looks like this: <asdf:foobar attr="value"> <child>...</child> </asdf:foobar> I forget what the asdf is called, but it is what's causing the problem. My normal method of traversing in XLinq doesn't work: xElem child = xDoc.Element("foobar"); Sets child to null because it...

Setting property while enumerating over IOrderedEnumerable<> doesn't set property on object in collection

I'm at a bit of a loss here. I'm creating a collection of objects from another collection using LINQs .Select and then using .OrderBy to get an IOrderedEnumerable. This part works fine, but when I loop over this with foreach and set a property on the object in the collection, the object in the collection is not modified. I'm writing a...