linq

Help converting multi DB SQL Query to LINQ

I have the below SQL Query, which returns what I expect, and I would LIKE to accomplish the same thing in LINQ but so far my results have been less then spectacular. The major hurdle, as I see it, is that the data is coming from 3 separate DB's. I was able to accomplish this in LINQ but it is extremely slow, see here. So, with out fur...

Updates in Linq To Sql

For updating records, instead of querying the context and updating each record individually, we currently use code that does DeleteAllOnSubmit on existing set of rows and InsertAllOnSubmit on new set of rows. This worked fine for majority of our scenarios, as in if the same row (content) is being inserted/deleted, it gets removed even th...

How can I merge two outputs of two Linq queries ?

I'm trying to merge these two object but not totally sure how.. Can you help me merge these two result objects? // // Create Linq Query for all segments in "CognosSecurity" // var userListAuthoritative = (from c in ctx.CognosSecurities where (c.SecurityType == 1 || c.SecurityType == 2) ...

Sum the difference of two columns in Entity Framework

I am trying to compute the sum of the difference of two columns: var result = model.TableExample .Where(condition here) .Select(s => s.Column1 - s.Column2) .Sum(); but it is rising an exception: Index and length must refer to a location within the string I can't understand th...

C# abstract base class for common columns in LINQ

This is what I have so far using System; using System.Collections.Generic; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Linq; using System.Text; namespace Firelight.Business { public interface IBaseEntity<K> { K Id { get; } } /// <summary> /// Base business database connection object...

what is the best linq query for this

I have a collection of Calendar objects. Calendar has a property Id. IEnumerable<Calendar> CalendarsToView; I have another collection of Event Objects. Event objects have a property of CalendarId IEnumerable<CalendarEvent> Events; i want to filter the Events collection to only return events where the calendarId is in the Calendar...

Filtering rows of a DataTable with the return type of a DataTable

I would like to filter the rows of a DataTable and then perform a LINQ query on the resulting set of rows. The second query currently operates on a DataTable.AsEnumerable. The DataTable.Select method returns an array of DataRows. Is there anyway to perform a LINQ query on these, or convert a the array of DataRows to a DataTable, so I can...

C# linq FirstOrDefault()

I select one double value from IEnumerable, how can i overload FirstOrDefault() function, to return null on default, instead of zero, iwant something like: double? x = from ... .FirstOrDefault(); i now i can catch exception, and write double? x = null, but i have 20 variables, and its not the way ...

Requirements for collection class to be used with LINQ

I always thought that enough requirement that class should satisfy to be able to use Where() with it is to implement IEnumerable. But today a friend of mine asked me a question, why he cannot apply Where() to the object of SPUserCollection class (it is from Sharepoint). Since this class is derived from SPBaseCollection which implements ...

How can I use linq to check if an flags/bitwise enumeration contains a type?

Hi folks, the following lambda statemement returns null, when i was hoping it would return a string value. var countryCode = AddressComponents .Where(x => x.AddressType == AddressType.Country) .Select(x => x.ShortName) .SingleOrDefault(); now the AddressType property of the current instance i'm interrigating contains the ...

converting/Casting ISingleResult - List values to DataTable without Looping.

Hi Everyboyd I am using Linq to Sql. When My StoredProcedure Executing its result ll returned in form of IMultipleResults. I am converting it to ISingleResults and It can be Casted as List. So I need to Convert it to DataTable so That I can Bind it to Dataset and Pass the values to UI. But I want such method where I can convert it wi...

Visual C# Express with DryadLINQ?

Can we use Visual C# Express (instead of Visual Studio) with DryadLINQ? We know that Visual C# Express 2010 supports LINQ. But I am not sure if DryadLINQ would also be supported, because Dryad might require PLINQ features also. If anybody can shed light, I would be grateful. Procuring a license for Visual Studio is a tedious process i...

how to split list using linq

i have List of int which consists of value 0,0,0,1,2,3,4,0,0 now i like to split this into 3 lists like this list A consists 0,0,0 and List B consists 1,2,3,4 and List C consists 0,0.I know how split using if and for,but how can i do this using linq. usual format i need split in starting some zeros and in middle some values and in last ...

How can I easily store my C# data structure(XML serialiazable) into a database?

I have a C# data structure which consists of classes and collections which have references to each other. Currently I am using Serialization to store the structure in XML, which works as a handy way to save/load the structure into my application. If I want to be able to save/load the structure into a database, is there a simple way? Sho...

How can I return if an optional navigation property exists from an Entity Framework Query?

I'm trying to return a boolean value using the following query: var q = from inmate in context.Inmates select new { inmate.Id, IsCrazy = inmate.Certified != null }; IsCrazy should be true only when the optional Certified navigation property is not null. However, IsCrazy is always being r...

Difference between DataView.Sort and DataTable.OrderBy().AsDataView()?

I have a DataGridView which is bound to a DataTable that contains a column of custom types that I need to sort by. This custom type (MyCustomType) implements IComparable<MyCustomType> which is how I wanted the sorting to work. I tried 2 methods to create a DataView which I would use as the grid's data source: MyDataTable dt = new MyDa...

Get the latest updated sub-directory

Am trying to find the latest sub-directory from a parent director. public static DirectoryInfo GetLatestSubDirectory(string parentDirPath) As of now the implementation is uses the bubble sort algorithm to find the latest by comparing the creation time. if (subDirInfo.CreationTimeUtc > latestSubDirInfo.CreationTimeUtc) Am wonderin...

How to handle no results in LINQ?

in this example code public Company GetCompanyById(Decimal company_id) { IQueryable<Company> cmps = from c in db.Companies where c.active == true && c.company_id == company_id select c; return cmps.First(); } How should I handle...

Expression.Call - Calling linq extension: FirstOrDefault, Where....

I am trying to create the following dynamically, however I am having problems calling the extension method "FirstOrDefault" using(var context = new Entities()) { var list = context.Engines.Include("Cars").Select(e => e.Cars.FirstOrDefault()).ToList(); } I have the following Expression parameter = Expression.P...

how to create this linq query

I have a situation where I have a list of ids such as {1,2,3,4}. I need to pass into a method another list of ids and if the list has the same numbers in it return true, else if either list is not identical (disregarding ordering) I need to return false. So, a call to the method with {1,2,3,4,5} should return false while a call with {2...