linq

Am I misunderstanding LINQ to SQL .AsEnumerable()?

Consider this code: var query = (from a in db.Table where a = SomeCondition select a.SomeNumber).AsEnumerable(); int recordCount = query.Count(); int totalSomeNumber = query.Sum(); decimal average = query.Average(); Assume "query" takes a very long time to run. I need to get the record count, total SomeNumbe...

Best way get first word and rest of the words in a string in C#.

In C# var parameters = from line in parameterTextBox.Lines select new {name = line.Split(' ').First(), value = line.Split(' ').Skip(1)}; Is there a way to do this without having to split twice? ...

Linq - Query a table and return results as XML

I have the following table structure: CREATE TABLE [Report].[MesReport]( [MesReportID] [int] IDENTITY(1,1) NOT NULL, [ParentID] [int] NOT NULL, [ReportTitle] [nvarchar](80) NOT NULL, [ReportName] [nvarchar](80) NOT NULL, [DatabaseServer] [nvarchar](80) NOT NULL, [DatabaseName] [nvarchar](50) NOT NULL, [Login]...

Collection Exists Criteria in WCF Data Services

I'm trying to ask my OData service: "Give me all the Products that do not have a Category" with the Products and Category have a m2m relationship. I've tried: from p in Products where p.Categories == null select p and from p in Products where !p.Categories.Any() select p and from p in Products where p.Categories.Count == 0...

Simple Transformation with Linq

I'm sure this is a trivial question but I couldn't find a good example. Suppose all you want to do is change one attribute for all objects in a list. I'd like to say something like: List<SomeType> list = ...; list.Select(x => x { x.Name = "Foo" } ); Notice the absence of the "new" keyword. I don't want to recreate objects that alread...

Custom function in Entity Framework query sometimes translates properly, sometimes doesn't

I have this function: public static IQueryable<Article> WhereArticleIsLive(this IQueryable<Article> q) { return q.Where(x => x != null && DateTime.UtcNow >= x.PublishTime && x.IsPublished && !x.IsDeleted); } And it works just fine in this query: from a in Articles.WhereArticleIsLive() where a.Id ==...

Convert DataTable to IEnumerable<T>

I am trying to convert a DataTable to an IEnumerable. Where T is a custom type I created. I know I can do it by creating a List but I was think there was a slicker way to do it using IEnumerable. Here is what I have now. private IEnumerable<TankReading> ConvertToTankReadings(DataTable dataTable) { var tankReadings = n...

what is the best way to structure this query

I have a IEnumerable collection of objects. The objects have a field LastedUpdated which is a DateTime field. I want to filter the collection, given a timestamp that I have, to return the record in the collection with that timestamp and the "next record" in time (based on this field) because I then want to have some code do a "diff" be...

Construct nested linq-to-sql expression so as to generate a single SQL query

I'm trying to construct my linq-to-sql expression so that it only generates a single SQL database query. The query involves two nested selects, a simplifed verion is: var query = from person in People where person.ID == 1234 select new PersonDetails() { ID = person.ID, ...

LINQ .Startswith or .Contains problems in VB.NET4

Hi! This may be a newbie question... In my code I can easily use "where Obj.Feld = String", but using "where Obj.Feld.StartsWith("a")" doesn't work. See the following two functions: Public Function EntriesByFileName(ByRef Database() As Entry, ByVal Filename As _ String) As IEnumerable(Of Entry) Dim Result As IEnumerable...

linq count/groupby not working

Hello all, I want to group by the categoryid and then do a count on this. But I don't know how to do this. I have tried a couple of ways without success. Here is my latest: public class Count { public int TradersCount { get; set; } public int Id { get; set; } public string Description { get; set; } } public IQu...

How can I create an Expression within another Expression?

Forgive me if this has been asked already. I've only just started using LINQ. I have the following Expression: public static Expression<Func<TblCustomer, CustomerSummary>> SelectToSummary() { return m => (new CustomerSummary() { ID = m.ID, CustomerName = m.CustomerName, LastSalesContact = // This is a Per...

c# linq distinct

Hello, I have the following code new Dictionary<string,IEnumerable<Control>>() { {"view1", new Control[] { contents, building, view1 }}, {"view2", new Control[] { contents, view2 }}, {"view3", new Control[] { building, view3 } }}); How do I get a list of...

Iterating tables in a context and the properties of those tables

I'm iterating the tables of a context and then the properties of those tables to eager load all columns in a context. I received some help via another question, but I don't seem to be able to figure out how to iterate the column properties of the actual table. Final working code: public static void DisableLazyLoading(this DataContext...

Linq to XML if/foreach with XElement

It seems I am having a bit of trouble with Linq to XML, I have looked for tutorials, but nothing really tells me about from, select, statements. I would like to know how to do a foreach/if statements with linq, if you have a tutorial please let me know. My problem right now is I only want a certain part put into my XML if the textbox h...

How to get the sum of list of shorts using the extension method Sum()?

I was trying to do something like this - List<short> listofshorts= new List<short>(); int s = listofshorts.Sum(); //this does not work...but same code works for a list of ints.. I got this compilation error - 'System.Collections.Generic.List' does not contain a definition for 'Sum' and the best extension method overload 'System.L...

Operator '=' is not defined for types 'Integer' and 'IQueryable(Of Integer)'

This is giving me a headache. I have this link query here that grabs an ID Dim mclassID = From x In db.SchoolClasses Where x.VisitDateID = _visitdateID Select x.ClassID And then later on I have this linq query ViewData("Staff") = From t In db.Staffs Where t.ClassID = mclassID Select t Any help would be much appreciated. I've tried ...

Tables in LINQ to Entities are not related

I'm working with asp.net 3.5 WebForms and LINQ to Entities. I created the database with their relationships, but when I go to import the data with LinqToEntities, some reports and have not seen any reports 1 to 1. Can anyone help me? I can create them after the reports have imported the tables LinqToEntities? ...

Have XML file need it to populatemultiple SQL Tables

I have a XML file that I need to populate multiple SQL tables, and I was wondering what the best way to do that is. I was thinking dataset, or xslt but I honestly not sure. Here is my generated XML (part of it) <?xml version="1.0" encoding="utf-8" standalone="yes" ?> - <!-- Created: 8/3/2010 12:09:15 PM --> - <Trip> - <TripDetail...

Get a distinct list of ids from IEnumerable<T>

I have an IEnumerable that I want to get all the distinct MaterialIDs. I have code that is working but I was wondering if there is a better way possible using LINQ. Here's the code I have: private IEnumerable<int> GetDistinctMaterialIDs(IEnumerable<TankReading> tankReadings) { var distinctMaterialIDs = new List<int>();...