linq

Faster SQL Inserts?

Hi, I'm dealing with chunks of data that are 50k rows each. I'm inserting them into an SQL database using LINQ: for(int i=0;i<50000;i++) { DB.TableName.InsertOnSubmit ( new TableName { Value1 = Array[i,0], Value2 = Array[i,1] } ); } DB.SubmitChanges(); This takes about 6 m...

Replacing if(x) Foreach() with Foreach.Where(x)

Probably a stupid question but I have a lot of: if(X) { foreach(var Y in myList.Where(z => z == 1) { } } constructs in some code Is replacing it with foreach(var Y in myList.Where(z => X && z == 1) { } insane? It is probably less readable, but will the compiler optimize it to make it pretty much the same code? ...

C# Reflection on Nested Properties of a Class

I wanted to know how can I get the Value of Property in C#, but this property is of another Type. public class Customer { public string Name {get; set;} public string Lastname {get; set;} public CustomerAddress Address {get; set;} } So I'm able to get the property values of Name and LastName but I quite don't get how to get t...

Using Linq not equals

I've 2 list collections in my C# app..A and B. Both the collections have customer object which has Id and Name attributes.Typically, A has more items than B. Using Linq,I want to return only those customers whose Id is in A but not in B. How do I do this? ...

IQueryable OfType<T> where T is a runtime Type

I need to be able to get something similar to the following to work: Type type = ??? // something decided at runtime with .GetType or typeof; object[] entityList = context.Resources.OfType<type>().ToList(); Is this possible? I am able to use .NET 4 if anything new in that allows this. ...

Linq comparing two list - learning linq

Hi I recently started learning LINQ. Bsically to understand this technology better I try to rewrite some of my previous programs using LINQ. I mean I try to replace foreach methods etc with linq queries. Today I encounterd a problem. I have a list of objects element List<Element> elementList public class Element { priv...

Selecting Items with the same subcollection using LINQ

Here is the pseudo case: class parent{ string name; //Some Property List<int> myValues; ....... } ........ //Initialize some parent classes List<parent> parentList = new List<parent>(); parentList.add(parent123); //parent123.myValues == {1,2,3} parentList.add(parent456); //parent456.myValues == {4,5,6} parentList.add(parentMa...

Convert an expression tree for a c# query expression

I am having a problem converting this query via an expression tree: WageConstIn => Convert.ToString(WageConstIn.Serialno).StartsWith("2800") This is my expression tree: var searchTextExp = LinqExpression.Constant("2800"); var parameterExp = LinqExpression.Parameter(typeof(WageConstInEntity), "WageConstIn"); var propertyExp = LinqE...

OData WCF Data Services - Related data (by foreign key) doesn't show up when calling the service

When I get the data using the URL in browser, I'm able to see the related data, for example: http://localhost/services.svc/Dinners(1)/RSVPs That lists 5 RSVPs for DinnerId = 1 in my case, but when I'm consuming the OData from a different project, I can only retrieve Dinners, debugging the app shows that RSVPs = 0, while it should be 5....

Get a random item from a collection that does not already exist in another collection - LINQ?

Hi, I am trying to learn LINQ but it is quite confusing at first! I have a collection of items that have a color property (MyColor). I have another collection of all colors (called AvailableColors - lets say 10 for example). I want to get a random color from the AvailableColors that does not already exist in my collection. My current...

LINQ Aggregate behavior of nullable types

Hi, Can someone explain what is going on here? Howcome both these things are true? [TestMethod] public void WhatIsGoingOnHere() { List<int?> list = new List<int?> { 1, 2, 3, null, 5, 6 }; Assert.AreEqual(17, list.Sum()); int? singleSum = 1 + 2 + 3 + null + 5 + 6; Assert.IsNull(singleSum); } Specifically, why does th...

Does Linq to Entities throw SqlExceptions?

Hi! I'm using linq to entities, and my entity model is sitting on top of MSSQL database. I'm wondering if linq to entities throws SqlExceptions or not. In other words, will the below code catch exception successfully if there was a problem connecting to the database? If it doesn't, what's the best way to handle exceptions when using lin...

LINQ to SQL - How to "Where ... in ..."

I want to use linq to sort a resultset. The resultset should contain all items which has it's code also in the given array. To make this a bit clearer, in sql this should be: select * from tblCodes where CodeSort in (0, 2, 3, 5, 6) Now I want to do the same thing in LINQ but I can't seem to pull it off.. I've tried Contains, but that ...

LinQ to collection a basic question.

This may be too simple.Please help. List<Line> listLines = new List<Line>(); foreach (Point p in currentPointsLines) { Line l = new Line(); l.Tag = p; l.X1 = AnotherList[(int)p.X].CenterX; //AnotherList is of type Rectangle l.Y1 = AnotherList[(int)p.X].CenterY; l.X...

Implement linq's Cast<T>() on single objects

How would you implement the Cast<T>() method of linq on single objects? Here's the scenario: public interface IFoo { String Message { get; set; } } public class Foo : IFoo { IFoo.Message { get; set; } internal SecretMessage { get; set; } // secrets are internal to the assembly } internal class Fubar { public IFoo Foo ...

Using LINQ to order a list using an existing array.

class Car{ int ID {get;set;} string Text {get;set;} } List<Car> allCars = new List<Car>(){ new Car{ ID = 1, Text = "one"}, new Car{ ID = 2, Text = "two"}, new Car{ ID = 3, Text = "three"}, new Car{ ID = 4, Text = "four"} }; int[] existingCarIds = new int[]{ 2, 4 }; I would like to have this list. CarWithID2 CarWithID4 CarWi...

Derived class causes InsertOnSubmit() to throw NullReferenceException

I did look at the related posts that were answered, but still get the NullReference exception in InsertOnSubmit(). I have an auto generated LINQ class Transaction from which I create a derived class that overrides ToString(): public partial class MyTransaction:DataAccess.Transaction { public MyTransaction():base() { } ...

LINQ where clause running against mapped object

If I want to run something like this BLL.Person person = (BLL.Person)repository.Single(item => item.Id == Id); Down in my single method I'd do something like this: public Resource Single(Expression<Func<BLL.Resource, bool>> where) { Resource resource = AsQueryable().FirstOrDefault(where); return resource; } protected IQuer...

Linq join on parameterized distinct key

I'm trying to LINQ two tables based on a dynamic key. User can change key via a combo box. Key may be money, string, double, int, etc. Currently I'm getting the data just fine, but without filtering out the doubles. I can filter the double in VB, but it's slooooow. I'd like to do it in the LINQ query right out of the gate. Here's the da...

Linq to SQL Cast Exception

Hi, I'm starting my first Linq to SQL project in VB.NET (which I am also new to). I am trying to Delete an entity but am having trouble with an InvalidCastException. The debugger breaks at the Next statement in the ForEach loop. My entity class is called Material. Any help would be very much appreciated. Thanks, Kenneth Di...