linq

LINQ deletion - Can delete one way, can't using Single enumerable

This works: var i = (from x in db.Test where x.Id == 1 select x).First(); db.Test.DeleteOnSubmit(i); db.SubmitChanges(); I get a cast error for this (int/string): var i = db.Test.Single(x => x.Id == 1); db.Test.DeleteOnSubmit(i); db.SubmitChanges(); I was also able to make an update using Single sucesssfully on the same ta...

Add two Lists of different length in c#

List<double> a = new List<double>{1,2,3}; List<double> b = new List<double>{1,2,3,4,5}; a + b should give me 2,4,6,4,5 obvisouly i can write a loop but is there a better way? using linq? ...

Question about LINQ and Lambda requery

I have a question about the result of LINQ and Lambda query. For example, I have the following codes: class ClassA<T> { public string Name { get; set; } public T ObjectT { get; set; } } List<ClassA<T>> list; // list is populated // First way to get instance from list, reference type? ClassA<T> instance1 = list.Select(x=> x).W...

How to merge a list of lists with same type of items to a single list of items?

The question is confusing, but it is much more clear as described in the following codes: List<List<T>> listOfList; // add three lists of List<T> to listOfList, for example /* listOfList = new { { 1, 2, 3}, // list 1 of 1, 3, and 3 { 4, 5, 6}, // list 2 { 7, 8, 9} // list 3 }; */ List<T> l...

What does LINQ return when the results are empty

I have a question about LINQ query. Normally a query returns a IEnumerable type. If the return is empty, not sure if it is null or not. I am not sure if the following ToList() will throw an exception or just a empty List<string> if nothing found in IEnumerable result? List<string> list = {"a"}; // is the result null or something e...

Converting Hibernate linq query to HQL

I understand that a IQueryable cannot be serialized. That means that queries can not be serialized, sent to a webservice, deserialized, queried and then sent back. I was wondering if it is possible to convert a hibernate linq query to hql to be sent over the wire. Is there another route I am missing? ...

Foreign Key in Entity Data Model

hello, I use Entity Data Model but I have aproblem: If a table e.g Customers have categoryId(foreign key from table category) it is not appear in cutomers entity???? so how can i retrive this value or set it? ...

LINQ single record forms or defaults if no record

I am new to LINQ and am liking it so far, however I am trying to load in some forms by requesting a single record (student). If this doesn't exist I want't default values i.e. empty strings false bools... so I used: db = new DataClassesDataContext(); student = db.ss_students.SingleOrDefault(p => p.student_id == studentID); txtR...

Checking if a XML child element exists with Linq to XML

Hi people, I'm trying to get my head around a problem I'm having in Linq to XML, seems like it should be pretty simple but even after browsing the Linq to XML questions here, I can't quite get it. Take something along the lines of the following XML: <users> <user id="1"> <contactDetails> <phone number="555 555 ...

LINQ Modelling Column Name Same As Table Name

Is there any way to name a column when LINQ modelling the same as the table? Such as this: [Table(Name="tblCC_Business")] public class Business { [Column(IsPrimaryKey=true, IsDbGenerated=false)] public string BusinessID { get; set; } [Column] public string Business { get; set; } } Our SQL table names don't necessarily reflect ...

ListView containing CheckBoxList - Selected items not showing as checked

I have a ListView with an EditItemTemplate that calls a method onItemEditing. Within my ListView I have a CheckBoxList bound using LINQ. In my onItemEditing method, I'm trying to check certain CheckBoxes if they are present in a look up table that links users with sectors. However, when I load the EditItemTemplate none of the CheckBox...

Compare lists and get values with Linq

Hello, My object is "MyClass" with 2 properties : Id (int) and HourBy (int) I have two list : var List1 = new List<MyClass>(); var List2 = new List<MyClass>(); I'd like to get one list with : - Object from List1 present in List2 based on Id with Hourby (list2) < Hourby in List1 - Object from List1 no present in List2 //#Sample1 //L...

Using Generics in Linq Query?

I am trying to write a generic function that generates an Xelement based on a list of objects, and a property of the object Currently I have this code copied and pasted in several spots InputElementsArray = New XElement(New XElement("ArrayInputs", _ New XElement("InputName", "TestFailedRefDesList"), _ ...

How to use Linq To Sql to count the idle working duration in a day?

For example, I have a Task MIS, the data is: Id User Task TaskStartTime TaskFinishTime 1 Mike task1 2009-07-28 09:00:00 2009-07-28 09:45:00 2 Mike task2 2009-07-28 09:30:00 2009-07-28 09:40:00 3 Mike task3 2009-07-28 09:50:00 2009-07-2...

C#/LINQ select sublist of objects with same properties

I'm a beginner with LINQ and I would like to know if it is possible to use it to solve the following problem: I have a class : public class myClass { public int Id { get; set; } public int Category { get; set; } public string Text { get; set; } } And I have a list of myClass objects. public List<myClass> myList; Can I easil...

Linq query works with null but not int? in where clause.

I have a linq query function like (simplified): public IList<Document> ListDocuments(int? parentID) { return ( from doc in dbContext.Documents where doc.ParentID == parentID select new Document { ID = doc.ID, ParentID = doc.ParentID, Name = doc.SomeOtherVar ...

LINQ: Get all selected values of a CheckBoxList using a Lambda expression

Consider a scenario where you want to retrieve a List or IEnumerable of the values of all the selected checkboxes in an <asp:CheckBoxList>. Here's the current implementation: IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() where item.Selected ...

Simple Sequence Generation?

I'm looking for an ultra-easy way to generate a list of numbers, 1-200. (it can be a List, Array, Enumerable... I don't really care about the specific type) Apparently .Net 4.0 has a Sequence.Range(min,max) method. But I'm currently on .Net 3.5. Here is a sample usage, of what I'm after, shown with Sequence.Range. public void ShowOut...

How to use NHibernate Projections to retrieve a collection.

I am lazy loading the collections, and also because there are so many fields within the person table, I am writing a projection function to retrieve only certain properties. It works with properties, just not collections of other entities. I would be fine if they were loaded in as proxies and i could get them later, but right now it just...

How to extract extra data from LinqDataSource?

I have a simple wizard generated query in a LinqDataSource like this: <asp:LinqDataSource ID="EvaluationsData" runat="server" ContextTypeName="Bonus.Models.BonusDataContext" Select="new (Id, Name, Coverage)" TableName="BM_Evaluations" </asp:LinqDataSource> I assign this data source to a DropDownList, using Id and Name as the Data...