linq

When searching for an item in a generic list should I use LINQ or Contains?

Hi All I have a generic List and i have to find a particular string in the list. Could you please let me know which is the best approach in the below ? List<string> strlist = new List<string>(); //Add strings to List if (strlist.Contains("Test")) { //string found } or string res = (from d in strlist ...

How can I call a method from within a LINQ expression?

I've my Model something maybe like this : public class Person { public virtual Guid ID { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } //this LazyList takes IQueryable<T> in constructor public virtual LazyList<Role> Roles { get; set;...

How to split list into LINQ groups and get biggest value in group

I have a List, and I would like to compute a double[360] containing the maximum range along each bearing (to the nearest degree). One hitch is that the chart plotting software I'm using requires each degree to be populated with no holes. I'm aware of the MoreLinq Batch extension, though not quite sure how to use it... Heres the code I ...

Entity framework linq query Include() multiple children entities

This may be a really elementry question but whats a nice way to include multiple children entities when writing a query that spans THREE levels (or more)? i.e. I have 4 tables: Company, Employee, Employee_Car and Employee_Country Company has a 1:m relationship with Employee. Employee has a 1:m relationship with both Employee_Car and Em...

What exactly is the point of Expression Trees?

Ok, I just don't get it. I've read about as much as I can on the subject without knowing what it's all about: Why use Expression Trees? What's a real-world example of when and how I'd use them? What overall benefit(s) are there in using them? ...

DRY LINQ Predicate Filters for Multiple Tables

Suppose I have two tables, TableA and TableB. Each record in A has one or more related records in B. Say I want a reusable filter using predicates. I might do something like this (Linq-to-SQL by the way): private Expression<Func<ARecord, bool>> FilterPredicate() { return x => x.Name == "Test"; } private IQueryable<ARecord> GetRecor...

Linq List comparing

Linq is great, but it always seems to confuse me a bit. This is my latest confusion: Say I have two List<String> objects. We will call them sourceList and destList. I need a way to find the list of strings that are in sourceList and not in destList AND find the list of strings that are in destList and not in SourceList. This is a bi...

Linq - Query a List<string[]>

How do you query a List<string[]> to get the index of the arrays having matches on their sub-arrays and get a return of type System.Collections.Generic.IEnumerable<string[]> ? EDIT: I have this: string[] report = File.ReadAllLines(@".\REPORT.TXT").AsQueryable().Where(s => s.StartsWith(".|")).ToArray(); List<string...

Linq to RSS feed?

What I'm trying to do is take an RSS feel URL and, using LINQ, be able to write a query that will let me sort the subject line of the feed or sort the author line of the feed or even do 'WHERE' clauses that will let me filter by keywords for example. I know I can read the RSS feed, parse each element, put them into some sort of class ob...

Sending command to SQL Database

Hey All, I am using WinForms & C# to access a SQL Server 2008 on my site which's hosted by winhost.com. I am using the following code to connect to the database, which I figured out for myself: try { SqlConnection scon = new SqlConnection( "My ConnectionString Info is in here."); scon.Open(); MessageBox.Show(scon.D...

Anonymous types query or normal query in LINQ

var emps = from x in DB where x.ID = 100 select x; var emp1 = from x1 in DB where x1.ID = 100 select new { x }; What is difference between these two queries. If we use anonymous-types is the performance will be increased or any other differences also there? ...

LINQ Contains Case Insensitive

This code is case sensitive, how to make it case insensitive? public IQueryable<FACILITY_ITEM> GetFacilityItemRootByDescription(string description) { return this.ObjectContext.FACILITY_ITEM.Where(fi => fi.DESCRIPTION.Contains(description)); } ...

Merge an Object that wen outside the datacontext

Hi, I have the following question: It is easy to insert an oBject in database with a form. Just create an object link it to the fields in your from. Post back to controller, create a new datacontext and do datacontext.InsertOnSubmit(object) . public static void AddPage(string lang, Page page) { ...

Casting and Linq Cast<T>()

When trying to answer this question, I discovered the following: string s = "test"; var result1 = s.Select(c => (ushort)c); // works fine var result2 = s.Cast<ushort>(); // throws an invalid cast exception Why does Cast<T>() fail here? Whats the difference? ...

Xdocument trying to create an XML file, having trouble with ListBox

So I have decided to use XDocument to create a XML file, which was working great until I came across a part where I have to find all the selected items in a ListBox. I am unsure how I should format this. XDocument xmlDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("...

Cannot Attach or Add an entity that is not new??

I have read the various posts regarding this error message and have typically avoided this problem in the past, but still haven't been able to figure this one, why am I getting this error: System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContex...

DynamicObject LINQ query does't works with custom class!!!

DynamicObject LINQ query with the List compiles fine: List<string> list = new List<string>(); var query = (from dynamic d in list where d.FirstName == "John" select d); With our own custom class that we use for the "usual" LINQ compiler reports the error "An expression tree may not contain a dynamic operation": DBclass db = new DBcl...

LINQ's Left outer join column selection

I have three tables from a dataset ds. var test0 = from a in ds.Tables[0].AsEnumerable() select a["ID"].ToString(); test0 has the following values -- [0] "8" [1] "9" [2] "11" [3] "2" [4] "1" var test1 = from a in ds.Tables[1].AsEnumerable() select a["SubscriptionID"].ToString(); test1 has ...

How to improve linq query and remove in clause

I have a linq query var query = from record in session.Query<Record>() from brwSet in session.Query<BorrowerSet>() from brw in session.Query<Borrower>() where brw.PrintOrder == 1 && brwSet.PrintOrder == 0 ...

Problem With Repository Pattern & Abstract Classes

Hi Guys, Come across a problem with the repository pattern combined with the use of abstract classes. I have a repository which implements a single method returning an ICollection of an abstract type. Here's my abstract class: public abstract class Location { public abstract string Name { get; set; } public abstract LocationTyp...