linq

Left outer join in LINQ is showing exception

I am getting the following exception: The null value cannot be assigned to a member with type System.Int32 which is a non- nullable value type. Below is my LINQ Statement where QuestionId is the primary key in my table: var questionViewsData = from questionViews in objDc.SC_QuestionsViews join questions in objDc.SC_...

Linq To Text Files

Hi All I have a Text File (Sorry, I'm not allowed to work on XML files :(), and it includes customer records. Each text file looks like: Account_ID: 98734BLAH9873 User Name: something_85 First Name: ILove Last Name: XML Age: 209 etc... And I need to be able to use LINQ to get the data from these text files and just store them in memo...

Search dynamically in fields of class(each time by other field)

hi all, In my 'Person' class, I have some fields like 'firstname','lastname','nickname' and so on. I want to write code to search dynamically, sometimes by 'firstname' and sometimes by 'nickname' field. In the regular way, the code will be: If(SearchBy == "firstname") { Person result = ListOfPerson.Where(p => p.firstname== "exp...

Insert XElements using LINQ Select?

I have a source piece of xml into which I want to insert multiple elements which are created dependant upon certain values found in the original xml At present I have a sub which does this for me: <Extension()> Public Sub AddElements(ByVal xml As XElement, ByVal elementList As IEnumerable(Of XElement)) For Each e In elementList ...

How do I return a Database table column using Expression in LINQ?

I want to return a column values dynamically at runtime using Expression<> how can I achieve this? What will be a function to return look like? I found this- http://www.onedotnetway.com/dynamic-sort-with-linq-to-sql/ but GetTable() in visual studio 2010 has no such function...Please help me out..Also I get this exception when using fun...

Is it possible to create ICriteria/ICriterion from LINQ or HQL?

I am creating a method that can create filter understood by NHibernate (by filter i mean a set of ICriteria object for example) from my abstract filter object. public static IEnumerable<ICriterion> ToNhCriteria(this MyCriteria criteria) { // T4 generated function // lots of result.Add(Expression.Or(Expression.Eq(),Expression.Eq)) ...

How to create custom get and set methods for Linq2SQL object

I have some objects which have been created automatically by linq2SQL. I would like to write some code which should be run whenever the properties on these objects are read or changed. Can I use typical get { //code } and set {//code } in my partial class file to add this functionality? Currently I get an error about this member alread...

Sort collection within collection using Linq

Hi, I have a one-to-many Linq query and I would like to sort on a property within the "many" collection. For example in the pseudo-code below, I am returned a List from the Linq query but I would like to sort / order the Products property based on the SequenceNumber property of the Product class. How can I do this? Any information is app...

LINQ Join 2 List<T>s

Preface: I don't understand what this does: o => o.ID, i => i.ID, (o, id) => o So go easy on me. :-) I have 2 lists that I need to join together: // list1 contains ALL contacts for a customer. // Each item has a unique ID. // There are no duplicates. ContactCollection list1 = myCustomer.GetContacts(); // list2 contains the cust...

"Attach or Add an entity that is not new...loaded from another DataContext. This is not supported." Problem on insert of linker object.

Similar error as other questions, but not quite the same, I am not trying to attach anything. What I am trying to do is insert a new row into a linking table, specifically UserAccomplishment. Relations are set in LINQ to User and Accomplishment Tables. I have a generic insert function: Public Function insertRow(ByVal entity As Impleme...

LINQ Single vs First

LINQ: Is it more efficient to use the Single() operator over First() when ever I know for certain that the query will return a single record? Is there a difference? ...

Sort and Group in LINQ

I apologise in advance if this has been answered before; it seems so fundamental to me yet I haven't turned up the answer in any of my searches. I have a list of string tuples, say (P1,P2) I'd like to know if there's a LINQ statement where I could group by P1 (in ascending order), and have that group contain all the P2 values for the g...

Need help for a complex linq query

Ok so I've got a DataTable here's the schema DataTable dt = new DataTable(); dt.Columns.Add("word", typeof(string)); dt.Columns.Add("pronunciation", typeof(string)); The table is filled already and I'm trying to make a linq query so that i can output to the console or anywhere something like : Pronunciation...

linq null parameter

how can i do the code static string BuildMenu(List<Menu> menu, int? parentId) { foreach (var item in menu.Where(i => i.ParentMenu == parentId || i.ParentMenu.MenuId == parentId).ToList()) { } } return BuildMenu(menuList,null); so if parentId==null then return only records i =>...

linq2sql and multiple joins

is it possible to do multiple joins: from g in dataContext.Groups join ug in dataContext.UsersGroups on g.Id equals ug.GroupId join u in dataContext.Users on u. where ug.UserId == user.Id select GroupRepository.ToEntity(g); in the sample above all is fine until i press "." in the end of the 3rd line. there i expect to get intellisense...

How to Select two values from one value

I want to return a collection of strings where every second record is "0" similar to this: foreach (Customer c in customers) { yield return c.Name; yield return "0"; } I started: customers.Select(c => new { ...

mysql and linq support

Hi All, I have read in many forums that there is no good linq for mysql (free) but his forums are 2 years old. In the meantime is there any good free linq for mysql ? Hog good is for professional use? Regards ...

Linq vs Stored Procedure

which one is preferable for Enterprise CMS development. LINQ or SP ...

anonymous type and multiple properties

I have this error: An anonymous type cannot have multiple properties with the same name. Whether this can be resolved with alias ie, whether an alias exists in LINQ var device_query = from d in DevicesEntities.device join dt in DevicesEntities.devicetype on d.DeviceTypeId equals dt.Id join l in DevicesEntities....

IEnumerable<> to IList<>

I am using Linq to query my database and returning a generic IList. Whatever I tried I couldn't convert an IQueryable to an IList. Here is my code. I cannot write simpler than this and I don't understand why it is not working. public IList<IRegion> GetRegionList(string countryCode) { var query = from c in Database.RegionDataSour...