linq

Enumerating Collections that are not inherently IEnumerable ?

When you want to recursively enumerate a hierarchical object, selecting some elements based on some criteria, there are numerous examples of techniques like "flattening" and then filtering using Linq : like those found here : link text But, when you are enumerating something like the Controls collection of a Form, or the Nodes collecti...

Why didn't see 10 records in database ?

I use JMeter to test my web application. and my test case is to add a record in database. and then I set the "number of thread user" to 20, it means that it will simulate 20 users work at the same time. and then I run the test case. But finally i found the system didn't create 20 records in database, but instead just create 13 records in...

SQL Linq Many To Many

I have a database with a User's and Role's table. Each user can be in lots of roles, so there is a 'middle' table called UserRoles that simply contains a RoleId and UserId. Now I need to get all the roles attached to a User. public IEnumerable<Role> GetAllRoles(int UserId) { var query = from R in _db.Roles ...

problem in linq with comma separation of string

I am using the following to convert comma separated string to list. string productId ="1,2"; string productName = "Product1,Product2"; string prodCat = "Cat1,Cat1"; string quantity = "10,10"; string retailPrice = "12,12"; var _productId = new List<String>(productId.Trim().Split(',')); var _productName = new List<String>(productName.Tri...

How can I create a LINQ-to-SQL statement when I have table name as string?

If I have the name of a database table like this: string tableName = "Addresses"; string tableName = "Customers"; How can I construct a dynamic LINQ statement like this: var items = from o in db.{tableName} select o; foreach (var item in items) { sb.Append(item.Id + Environment.NewLine); } I know I could do something like t...

Comparing IPAddress (stored as varbinary)

I have an IPAddress column on my Activity table. This is stored as a varbinary(16) so that it can be efficient (moreso than storing as a string) and also support IPv6. When I store, I basically get the value of (new System.Net.IPAddress("127.0.0.1")).GetAddressBytes(). What I want to be able to do is search for all IP addresses that beg...

Pulling the WHERE clause out of LINQ to SQL

I'm working with a client who wants to mix LINQ to SQL with their in-house DAL. Ultimately they want to be able to query their layer using typical LINQ syntax. The point where this gets tricky is that they build their queries dynamically. So ultimately what I want is to be able to take a LINQ query, pull it apart and be able to inspec...

What is the term "deferred query evaluation" in the context of LINQ referes to?

What is the term "deferred query evaluation" in the context of LINQ referes to? (please give example). ...

Dynamically Modifying a LINQ to SQL Select Statement's Columns

Hello, I'm trying to build a REST-ful API for my app. Currently I have something like this: www.example.com/submissions/?format=json This will return latest ten submissions in JSON. Each object has its details, such as submission name, date created, user, body, etc. I'd like to do something such as: www.example.com/submissions/?format...

LINQ Dynamic Where - Not adding clause

Hello, I have the following code: public OTestTable GetTestCode(Func<TestTable, bool> whereClause) { return CoreContext.TestTables.Where(whereClause).Select(TestTableMap.DataToObject).FirstOrDefault(); } CoreContext is my data context (which is initialized in a base class) My TestTableMap is as follows: public class TestTableMa...

How to use LINQ-to-Entity to query by contained objects

Let's say I have a list of Boxes and in a box you can have multiple items. Box (id) Items (id, boxId) I'm trying to build a linq to entity query that can return all the boxes that contains ALL specified items. List<Box> FindBoxContainingAllSpecifiedItems(List<int> itemIds) { var q = from box in ctx.Boxes where ??? } ...

Return an empty collection when Linq where returns nothing

I am using the below statement with the intent of getting all of the machine objects from the MachineList collection (type IEnumerable) that have a MachineStatus of i. The MachineList collection will not always contain machines with a status of i. At times when no machines have a MachineStatus of i I'd like to return an empty collection...

return string[] from LINQ IQueryable object?

I'm trying to work with the .NET AJAX autocompletion extension. The extension is expecting the following... public static string[] GetCompletionList(string prefixText, int count, string contextKey) My database queries are in a LINQ var object. I'm getting compile-time errors about not being able to convert type IQueryable to string[]....

Converting Linq to XML query from C# to VB.Net. Can you spot my error?

I'm converting the Linq query below from C# to VB.Net. Can you spot my error? The query joins 3 XML datasets. Thanks in advance! C# - This one works great. List<Course> courses = (from course in CourseXML.Descendants(ns + "row") join coursecategory in CourseCategoryXML.Descendants("Table") on (string)course.Attribute("code") equals...

Rewrite this foreach yield to a linq yield?

Say I have the following code (context narrowed down as to keep the question scope limited) public static IEnumerable<Color> GetThemColors(){ var ids = GetThePrimaryIds(); foreach (int id in ids){ yield return GetColorById(id); } ids = GetTheOtherIds(); foreach (int id in ids){ yield return GetOtherColorsById(id); } } I woul...

How many objects can LINQ used to create per second ?

I used Linq to insert objects into database.But if i used threads to simultanously create 20 object within 1 second, then system will fail to add 20 objects into database. And I found it is not because of the sql server 's limit. so the only possible is Linq, any one have idea ? How can I create 20 records or more in 1 second within 1 s...

Return null for FirstOrDefault() on empty IEnumerable<int>?

Say I have following the following snippet (context narrowed down to limit scope of question) int? nullableId = GetNonNullableInts().FirstOrDefault(); Because GetNonNullableInts() returns ints, the FirstOrDefault will default to 0. Is there a way to make the FirstOrDefault on a list of ints return a null value when the list is empty? ...

Better way of searching through lists than using foreach

list vclAsset<FullAsset> list callsigns<string> foreach(FullAsset fa in vclAsset) { if (callsigns.contains(fa.asset.callsign)) { //do something } } Is there a more elegant way to do the above? A FullAsset object contains an Asset object which in turn has a string "Callsign." Each callsign will be unique, so my li...

Lambda syntax in linq to db4o?

I know the following is possible with linq2db4o from Apple a in db where a.Color.Equals(Colors.Green) select a What I need however is something that allows me to build my query conditionally (like I can in other linq variants) public IEnumerable<Apple> SearchApples (AppleSearchbag bag){ var q = db.Apples; if(bag.Color != null...

WCF Linq to SQL Table - System.Data.Linq.Table cannot be serialized.

I can't figure this out as I go through demos that seem to work. I have a WCF service I was trying to use Linq to SQL with. However, all I ever get is the error System.Data.Linq.Table cannot be serialized. So I started with my own class thinking I could build it back up until get the error. Problem is I get the error even trying to use ...