linq

Entity Framework - Select specific columns and return strongly typed without losing cast

I'm trying to do something similar to this post where I don't pull back all columns from a particular entity, however my framework makes use of inheritence and I lose scope of the entity type after it's been cast to an anonymous type. The structure of my Entity Framework has a base entity called Action. From here I've created two inhe...

How to treat a Linq to Entities query ?

I have this code: public article GetArticleByWebSite(string webSite) { using (var context = new ceopolandEntities()) { return context.article.Where(a => a.WebSite == webSite).First(); } } What's the best way to chceck if article isn't empty before calling First() ? A try catch block or ...

Issues with de serializating PLINQO objects in C#

I have tried many ways to deserialize LINQO objects but every method failed. Here is an example of the last code. System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(SalesNetData.Country)); string xmlData = Session["CCC"].To...

Best way to connect gridview to data source

My DAL is created using Linq and all is fine. However, on one page, I'm to display a table (gridview) which pulls data from a SQL Server database table. However, I've heard many warnings about staying away from using any of the built in controls (ObjectDataSource and SQLDataSource). Is there any truth in this, regarding scalability/effic...

Using LINQ to Objects to find items in one collection that do not match another

I want to find all items in one collection that do not match another collection. The collections are not of the same type, though; I want to write a lambda expression to specify equality. A LINQPad example of what I'm trying to do: void Main() { var employees = new[] { new Employee { Id = 20, Name = "Bob" }, new...

Help needed for optimizing linq data extraction

I'm fetching data from all 3 tables at once to avoid network latency. Fetching the data is pretty fast, but when I loop through the results a lot of time is used Int32[] arr = { 1 }; var query = from a in arr select new { Basket = from b in ent.Basket where b.SUPERBASKETID == ...

When using LINQ, what is the difference between && and multiple where clauses?

I am new to LINQ and discovered yesterday that you can have multiple where clauses such as: var items = from object in objectList where object.value1 < 100 where object.value2 > 10 select object; Or you can write: var items = from object in objectList where object.value1 < 100 && object.value2 > 10 select object; What...

LINQ to XML Question: Returning Nodes with a particular select

Hi, I have this XML: <?xml version="1.0" encoding="utf-8"?> <ConfiguraCanale ID_Comando="1"> <canaleDigitalOUTPUT ID_Canale="1" > <stato>0</stato> </canaleDigitalOUTPUT> </ConfiguraCanale> <ConfiguraCanale ID_Comando="2"> <canaleAnalogicoINPUT ID_Canale="2"> <timeAttesaPreCamp>00:03:00</timeAttesaPreCamp...

Why LINQ to SQL does not work with silverlight vb, but if successful c #?

Why LINQ to SQL does not work with silverlight vb, but if successful c #? ...

Need help creating Linq.Expression to Enumerable.GroupBy

I am attempting to generate an Expression tree that ultimately calls a series of GroupBy methods on the Enumerable type. In simplified form I am attempting something like this: IEnumerable<Data> list = new List<Data>{new Data{Name = "A", Age=10}, new Data{Name = "A", Age=12}, new Data{Name = "B", Age=20}, new Data{Name="C", Ag...

Identify source of linq to sql query

We are starting to have numerous linq to sql queries in our code. We have started to pay more attention to performance and are starting to see queries that we think are coming from linq. They have the t1, t2...tN values, so we are sure they are linq generated. However, we are having difficulty determining the location in code that is the...

LinqToSql query that spans a many-to-many relation?

Let say i have the following schema Content(Id, ....) TagContent(TagId, ContentId) Tag(TagId, Name) Suppose I'd like to select all content records that have tag with name "test". In SQL I would write: select Content.Id from Content join TagContent as TC on (TC.ContentId = Content.Id) Join Tag on (TC.TagId = Tag.Id) w...

Removing the WHERE clause in a Linq query

I have a table of product information with many bit columns. This table can be queried from an interface which has a checkbox for each column. The checkboxes are grouped into several related groups. For example, three of the columns describe the products suitability for various markets, being Automotive, Aviation and Marine. If none of...

How to sort DataTable by 2 columns, with NULLs, maybe using LINQ?

I have an ADO.Net datatable that I need to sort by first by column1 then by column2, either of which may have nulls. Once sorted I need to read some values out of the rows and add to a listview. I have written code to do this DataTable.DefaultView.Sort (run twice). But wondering if there might be a better way. I was thinking maybe ...

C# Count() Extension Method Performance

If the LINQ Count() extension method is invoked on an IEnumerable<T> that has a Count property (e.g. List<T>), does the Count() method look for that property and return it (rather than counting the items by enumerating them)? The following test code seems to indicate that it does: using System; using System.Collections; using System.Col...

Sql Server table not updated with LINQ

I have a database that I have restored on SqlExpress from a bak file which was located on my SQL Server Pro instance. I am using Linq to interact with the database and am able to query and update some of my tables (i havent tried them all). However, I have at least one table that will not accept any kind of update (my UserAddress table)....

Optimal LINQ query to get a random sub collection - Shuffle

Please suggest an easiest way to get a random shuffled collection of count 'n' from a collection having 'N' items. where n <= N ...

a replace question

I have this string aa**b**qqidjwljd**p**fjem I need to replace b by p and p by b aa**p**qqidjwljd**b**fjem the way I do this look like this myvar.replace("b","1").replace("p","b").replace("1","p") this is kind of really ugly is there a better way? edit why ugly? because I have to decide/find an arbitrary set of characte...

Linq: How to query items from a collection until the sum reaches a certain value

Given the following object: public class Product { string Name {get;} int Quantity {get;} } using Linq, how would I query a List<Product> until I got a sum >= a given quantity? In other words, if my list looked like Name Quantity ----------------- prod1 5 prod2 6 prod7 7 I want to query the List and pul...

dynamic linq Like

How to write a dynamic linq method for Like clause. For.: the reference for Dynamic linq orderby is http://stackoverflow.com/questions/41244/dynamic-linq-orderby. I am looking for a similar one for dynamic Like clause. I have the following extension methods for like: public static IQueryable<T> Like<T>(this IQueryable<T> source, str...