linq

How to make IQueryable<out T> work with a reqular IQueryable?

I am using Linq2SQL and working with a legacy system. The system was built in .Net but without a datalayer so I am transcoding it so that it will have one. I have a linq query that looks like this... var data = from p in db.tblPeoples orderby p.strLastName,p.strFirstName select new ...

Search a MembershipUserCollection

Hi all, I am currently looking for a way to search though a MembershipUserCollection. At the moment the user will pick the role they wish to see. this could return 100's if not 1000's of records which are paged in a repeater. on the same screen the user can type in part of the user name they wish to find and it should filter the data...

Using LINQ, select list of objects inside another list of objects

public class ClassA { public string MyString {get; set;} } public class ClassB { public List<ClassA> MyObjects {get; set;} } List<ClassB> classBList = new List<ClassB>(); var results = (from i in classBList select i.MyObjects).ToDistinct(); I want a distinct list of all the ClassA objects in the classBList. How do I go abo...

encapsulating logic in a linq to sql query via extension method.

Given two classes in your LINQ to SQL .dbml file with the following properies. Customer CustomerId FirstName LastName AddressId Address AddressId Street City State Zip You could construct a LINQ query such as the following. using(var db = new MyDataContext()) { results = db.Customers ....

C# HashSet union on IEnumerable within LINQ Func expression does not work (possible precompiler bug)

I am using using Microsoft .NET Framework 4.0. I have run into this using Aggregate on a Dictionary<T, List<T>> to extract the set of type T values used across all type List<T> lists in the dictionary. Here is the simplest case I could come up with that exhibits the same behaviour. First, as the documentation states, the following does...

IsNull or Coalesce functionality in LINQ?

I have the following LINQ query: return (from r in Repository.Query<Measurement>() where r.Postal.ToLowerInvariant() == (string.IsNullOrEmpty(postalCode) ? r.Postal : postalCode).ToLowerInvariant() && r.Trait.ToLowerInvariant() == (string.IsN...

Linq, inserting record into cross reference table, hotfix not working

I have a cross reference table RolePrivilege that has FK to Role and Privilege tables....basically Role can have many Privileges. Hotfix located at Microsoft hotfix has already been applied. Here is my code: Public Sub InsertRolePrivilege(ByVal inrole As Role, ByVal inprivilege As Privilege) Dim r As Role = (From ro In DataC...

Dynamic Linq and substitution values with unknown number?

Typically a dynamic linq query with string can use a substitution value such as: result = db.Persons.Where("Name == @1", "John"); I have an unknown number of strings that I want to pass into the Where clause. I have no problem with integers, but the API cannot seem to handle a string without a substitution value. Does anyone kno...

Linq and order by

I have a generic class which could use a generic OrderBy argument the class is as follows class abc<T> where T : myType { public abc(....., orderBy_Argument ){ ... } void someMethod(arg1, arg2, bool afterSort = false) { IEnumerable<myType> res ; if ( afterSort && orderBy_Argument != null ) res = src.E...

What is the difference between these two LINQ queries?

Looking at the profiler I see a few differences. The second query which uses the include will in fact return data from related to the secondary table CountryCodes. This part makes sense to me. I don't however understand why this query has two joins. First it does a regular inner join between CountryCodes ands CountyCodeTypes (on the ...

Which is faster in execution or is more optimized? C#

I'm not sure if it's ok to ask this kind of question here, but I just want to know the difference between the two code snippets. As I was browsing the questions here in SO, I found this post: How to find the number of HTML elements with a name that starts with a certain string in c#? a user answered this: var dictionary = Request.Form...

Linq return all columns from all tables in the join

Hi, Lets say I have 2 tables both of them contain dynamic columns and I wish to retrieve a collection of datarow with all the columns from both the tables(later i will bind it to a grid view) after performing left outer join. Sample Query: var query = from TableA in ds.Tables[0].AsEnumerable() join TableB in ds.Tables[1].A...

Distinct on a specific field

I have a class structure: class MyEx{ public int Prop1; public int Prop2; public int Prop3 } Prop1 and Prop 2 are always the same , Prop3 varies. this class I want to retrieve from a longer the end should be something like select new MyEx { Prop1=something; Prop2= something2; Prop3=something3; } ...

LINQ to XML - Get element based on a nested elements value

I want to select an element within my XML based on the value of a nest element. Here is an example of the XML: <Agents> <Agent ID="xxx"> <Login>xxx</Login> <Password>xxxx</Password> <Products> <Product ID="zzz"> </Product> </Products> </Agent> </Agents> Here is my first ...

Left Join If A Certain Field Is A Certain Value?

I'm having a difficult time building a left join query that does something like this: var Results = from a in Db.Table1 join b in Db.Table2 on a.Id equals b.Id into c //IF b.Value2 == 1 from d in c.DefaultIfEmpty() select new {Table1 = a}; The problem I'm having is that I simply can't add "where b.Value2==1" before the...

Return Type for Collection from LINQ Query

I have a method, that returns a group of accounts Public Shared Function GetAllNotesByUser(ByVal UserID As Guid) As Account (??) Using db As New MyEntity Dim query= (From A In db.Account _ Where A.aspnet_Users.UserId = UserID _ Select A) Return query End Using End Function ...

Is there anyway I could use nullable datetime property to get the year ?

I am trying to perform the following linq to entities query- var data = DataContext.Employee.Where(e=>e.Date.Year>1986).ToList(); Now the problem is that Date field is a nullable DateTime field and I can't access Year property and on the database side I can't change legacy code! ....anyways is there anyway possible to use the year pr...

Use LINQ to get items in one List<>, that are not in another List<>

I would assume there's a simple LINQ query to do this, I'm just not exactly sure how. Please see code snippet below, the comment explains what I'd like to do: class Program { static void Main(string[] args) { List<Person> peopleList1 = new List<Person>(); peopleList1.Add(new Person() { ID = 1 }); peopleL...

SQLMetal generates code from stored procedures incorrectly.

sqlmetal wrongly generates code for my sproc as ISingleResult. I need multiple results! Check this out. SqlMetal actually generates ISingleResult for a sproc like this: CREATE PROCEDURE GetDetailReportData AS BEGIN select * from Log END How do you get sqlmetal to generate with multiple results for a sproc? Does anyone ever use sproc...

Basic question about OOP Class Structure with LINQ

I have this situation: public class busOrder: IbusOrder { public Order vOrder { get; set; } MyDataContext db = new MyDataContext(); public busOrder(int pOrderId) { vOrder = db.Orders.SingleOrDefault(p => p.Id == pOrderId); } public int SaveNew() { ... } public int GetStatus() {...