linq

What is wrong with putting Using Blocks in Repository?

I have using blocks in each method of my repository. If I want to cross reference methods, it seems it would be against best practices to initialize another Datacontext What am i doing wrong? If I declare a Datacontext in the class instead of using blocks in methods will I not lose power to dispose ?? public IList<something> GetSomethi...

Removing characters from strings with LINQ

I'm trying to brush up on my LINQ by writing some simple extension methods. Is there any better way to write such a function as below that removes a given list of characters from a string (using LINQ)? It helps me to think of the extension methods that LINQ relies on first: public static string Remove(this string s, IEnumerable<char> ...

Need help in resolving error in predicates for And operator in LINQ

Hi, I got into problem of predicate And operator. Code is : SQLDBDataContext sqlDS = new SQLDBDataContext(); Expression<Func<User,bool>> pred = null; //delcare the predicate to start with. if (Request["Name"] != null && ! Request["Name"].Equals(string.Empty)) { pred = c => ( c.ContactFirst.Contains(Request["Name"]) || c.ContactLa...

How to Eager Load entire SQL table in LINQ?

Writing my first Linq application, and I'm trying to find the best way to do the following: I want to load the entire employees table at once to populate the cache (used for form autocomplete). I can do - var query = from employee in db.Employees select employee; foreach (Employee e in query) { ... } But since this is deferred...

LINQ Incompatibility Issue with SQL Server 2000

Hello, I have a Linq to SQL query that was working just fine with SQL Server 2005 but, I have to deploy the web app with a SQL Server 2000 and, when executing that query, I get his error: "System.Data.SqlClient.SqlException: The column prefix 't0' does not match with a table name or alias name used in the query." I have more queries b...

LINQ to SQL-- Best learning resources?

It's not that I can't google for myself, however others have tread this path before me and I"m always interested in what other programmers have found helpful, or not :) Thanks in advance for any input. ...

How do I convert from a Dictionary to a SortedDictionary using LINQ in C#?

I have a Dictionary<string, double> and I want to convert it to a SortedDictionary<double, string>. How do I do this using LINQ extension methods in C# 3.0? EDIT: Generic angle brackets not in original question when Marc and Jared answered. ...

Which versions of SQL Server does LINQ to SQL support?

Can SQL Server 2000 be used as the database for LINQ to SQL? Does LINQ to SQL rely on a specific version of Microsoft SQL Server? ...

Exceptionally slow database response time on Linq query

I have written what I thought to be a pretty solid Linq statement but this is getting 2 to 5 second wait times on execution. Does anybody have thoughts about how to speed this up? t.states = (from s in tmdb.tmZipCodes where zips.Contains(s.ZipCode) && s.tmLicensing.Required.Equals(true) ...

C# method call delegation

Say I have a DLL assembly, containing an auto-generated class with two methods: public class AutoGeneratedClass { public int DoSomething(bool forReal) { ??? } public void DoSomethingElse(string whatever) { ??? } } The methods could be anything, really. The above is just an illustration. What kind of code would I need to gener...

Resolving extension methods/linq ambiguity

I'm writing an add-in for ReSharper 4. For this, I needed to reference several of ReSharper's assemblies. One of the assemblies (JetBrains.Platform.ReSharper.Util.dll) contains a System.Linq namespace, with a subset of extension methods already provided by System.Core. When I edit the code, it creates an ambiguity between those extensio...

How to Count Duplicates in List with LINQ

I have a list of items John ID Matt ID John ID Scott ID Matt ID John ID Lucas ID I want to shove them back into a list like so which also means I want to sort by the highest number of duplicates. John ID 3 Matt ID 2 Scott ID 1 Lucas ID 1 Let me know how I can do this with LINQ and C#. Thanks All EDIT 2 Showing Code: List<g...

Submitting a list of Items to Database with Linq datacontext

I have a list of values that I am looping through, determining whether each item exists in database table and then either updating or creating new row. Each time I use dataconext.submitchanges. I also need the ID for the newly created row which i get after submitchanges. Is there a better wa to do this than to connect on each cycle? ...

LINQ performance implications

I've got a simple procedure to strip a string of all characters illegal in XML: string SanitizeXml(string xml) { return string.Concat (xml.ToCharArray().Where(c => IsLegalXmlChar(c)).ToArray()); } It's nice and terse. But I'm concerned about its performance. The same thing could easily be accomplished using a simple for lo...

Linq and retrieving primary key

This code works, but i dont understand why. With DeferredLoadingEnabld = false, I would expect it not to return the primary key. Can someone explain what I am missing? public void SaveOrder (Order order) { using (DataContext dc= new DataContext) { dc.DeferredLoadingEnabled = false; ... or...

Is there a way to create an Linq XElement iterator that is updated as the tree is modified?

I'm new to both C# and Linq. I'm attempting to use a Linq-generated element collection to iterate over and modify certain elements in a XDocument. My understanding is that the enumeration does not update when the tree is updated. For example, given the document <root xmlns:ns="http://dummy"&gt; <ns:a/> <ns:b/> <c/> <ns:a><ns:a/></ns:...

Determining scope of a MemberExpressions target

Is there anyone here with experience writing custom Linq proviers? What I'm trying to do is tell whether a MemberExpression that is a property on a business object should be included in the sql, or treated as a constant, because its from a local variable that just happens to be a business object. So for example, if you have this: Cust...

How does LINQ defer execution when in a using statement

Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new MyDataContext) { return dc.tablename.Select(row => row.parameter == a); } } private void UsingFunc() { var result = MyFunc(new a()); foreach(var row in result) { //Do something } } According to the do...

linq to sql LoadWith limiting fields returned

Is there a way to use LoadWith but specify the fields that are returned? For example, if I have two tables 1) Products 2) Categories and do something like DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<Products>(d => d.Categories); db.LoadOptions = dlo; MyDataContext db = new MyDataContext(); var result = from d in db.Prod...

foreach to linq tip wanted

How do I do this in linq? var p = new pmaker(); foreach (var item in itemlist) { var dlist = new List<Dummy>(); foreach (var example in item.examples) { dlist.Add(example.GetDummy()); } p.AddStuff(item.X,item.Y,dlist); } // .. do stuff with p ...