linq

Want to return the entire contents of an XElement as a string

I'm a newbie to LINQ in C# and am using it to read in and work with XML files. I'm able to navigate up and down my elements, but what I want for some elements is to return the entire contents as a string. Meaning, I have an element like this: <element1> <subel1> some text here </subel1> </element1> When I get the value of elemen...

Updating multiple rows Linq vs SQL

Some time ago I wrote a piece of code to update multiple rows in a database table. The code was like this var db = new MyDataContext(); db.Execute("UPDATE Details SET IsActive = 0 WHERE MasterId = 1"); Then the other day when I got the latest version of the file I saw that somebody changed the code to something like this var details ...

Another LINQ Pivot Problem - Convert SQL Script to LINQ

There are a few questions on SO already regarding LINQ pivots and while a couple of them outline my exact problem, I can't successfully translate them to a working solution. I feel that this is mostly due to a join in my tables. So for the benefit of all the LINQ junkies out there who love a problem, here's another puzzle for you to wo...

LINQ Query to Return multiple results

I am trying to write a textbox that will search on 5 DB columns and will return every result of a given search, ex. "Red" would return: red ball, Red Williams, etc. Any examples or similar things people have tried. My example code for the search. Thanks. ItemMasterDataContext db = new ItemMasterDataContext(); string s = tx...

Newbie LINQ Question: Is Paging in LINQ Queries Possible?

Is it possible to using "paging" functionality in Linq queries? Let's say I have some XML like this: <Root> <BetaSection> <Choices> <SetA> <Choice id="choice1">Choice One</Choice> <Choice id="choice2">Choice Two</Choice> <Choice id="choice3">Choice Three</Choice> ...

Linq: multiple relationships to same table

I have the same problem as this guy: I have a table that has references my tblstaff table twice for two different people. Now that I have added this second reference neither of them work. What is up w/ that? ...

LINQ Subquery

I need to perform a LINQ query for both a time period from a database table (Period) and a list of invoices from a table (Invoice) that fall within the period's start and end dates. There is no key reference between the two tables, how can I do the Invoice subquery? I am trying to do something similar to the following: var query = (fr...

Custom Linq Extension Syntax

I have written a function that gets a given number of random records from a list. Currently I can do something like: IEnumerable<City> cities = db.Cites.GetRandom(5); (where db is my DataContext connecting to a SQL Server DB) Currently, I have a function like this in every entity I need random records from: public partial class Cit...

LINQ across multiple databases

I've got two tables that need to be joined via LINQ, but they live in different databases. Right now I'm returning the results of one table, then looping through and retrieving the results of the other, which as you can guess isn't terribly efficient. Is there any way to get them into a single LINQ statement? Is there any other way to co...

Easiest way to get a common base class from a collection of types.

I'm building a custom property grid that displays the properties of items in a collection. What I want to do is show only the properties in the grid that are common amongst each item. I am assuming the best way to do this would be to find the the common base class of each type in the collection and display it's properties. Is there any e...

a newbie question on Linq To Sql

When I do this manually public class AdventureWorks : DataContext { public AdventureWorks(string connection) : base(connection) { } public Table<Contact> Contacts; } [Table(Name = "Person.Contact")] public class Contact { [Column(DbType = "int not null", IsPrimaryKey = true, IsDbGenerated = true)] public int ContactID;...

How do you create optional associations in Linq-To-Sql classes?

I am trying to creating an optional association between a couple of tables. I have one table called Invoice. The Invoice table has a FK reference to the Customer table through the CustomerId field. The Invoice table also has a not enforced FK reference to the Project able through the ProjectId field. Is there anyway to set up my Linq...

Combine two LINQ queries?

Hello, I think I am having a mental block but can someone please enlighten me on how to combine these two LINQ statements into one? /// <summary> /// Returns an array of Types that implement the supplied generic interface in the /// current AppDomain. /// </summary> /// <param name="interfaceType">Type of generic interface implemented...

how do I pass null to int column in linq

Hi experts, How do we assign null value to int column in LINQ. eg. SQLDBDataContext sqlD = new SQLDBDataContext(); var result = from p in sqlD.loadsRadius(Convert.ToInt32(Request["radius"]), ..... here if Request["radius"] is null gives an error. I could pass 0 via this but I need to change in many already existing procedures. Th...

linq to xml: how to select the value from elements

Hi all, I need return a list of the element <AssetText>. My query below only returns back the first AssetText. Any thoughts much appreciated. var q = from c in xDoc.Descendants("Product") where (int) c.Element("ProductView").Element("ViewId") == 44 select (string) c.Element("ProductView").Element("AssetText").Element("T...

Encapsulating LINQ select statement.

I have LINQ statement that looks like this: return ( from c in customers select new ClientEntity() { Name = c.Name, ... }); I'd like to be able to abstract out the select into its own method so that I can have different "mapping" option. What does my method need to return? In essence, I'd like my LINQ query to look like this: return...

Hanging Linq query with Guid.Empty in the where expression

I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using (var ctx = ContextManager<Gimli.Data.GimliDataContext> .GetManager(Database.ApplicationConnection, false)) { this.RaiseListChangedEvents = false; this.IsReadOnly = ...

Linq to sql error with identitiy increment field

I've just started to use linq to sql and have run into a problem with inserting a record with an auto incrementing field. I have created a new instance of a company object defined by linq. it has initialised an auto incrementing field 'companyID' to 0. InsertOnSubmit() fails with the following invalidOperationException. Incorrect au...

master detail binding using wpf/linq to sql

I'm trying to create a master detail view of a linq to sql relationship in wpf. The view will have two comboboxes. One for selecting the master item and another for selecting the detail item. I have achieved this using an ADO.Net Dataset containing two tables and a relationship between the tables. where the first combobox binds to the ...

How would I get the column names from a Model LINQ?

I am looking to get a list of the column names returned from a Model. Anyone know how this would be done, any help would be greatly appreciated. Example Code: var project = db.Projects.Single(p => p.ProjectID.Equals(Id)); This code would return the Projects object, how would I get a list of all the column names in this Model. Thanks...