linq

Using First() with OrderBy and DynamicQuery in One-To-Many related tables

Note: I realize this question is similar to another question, however that question was asked in the context of Genom-e and remains unanswered. My question is in the context of LINQ DynamicQuery. I'm using the String extension method overload for OrderBy provided by System.Linq.Dynamic. Passing a String Literal to OrderBy works g...

Removing objects contained in a List<> from another List<>?

I have an object which is contained within a List<>, I need to remove these from another List<> e.g. List<MyObject> AllElements = new List<MyObject>(); List<MyObject> SearchResults = new List<MyObject>(); ... Do something so that SearchResults contains a subset of the objects contained within AllResults Currently I do this to de...

Dynamicallly building Linq where clause....

Hi, I am trying to do something that can be done conventionally by writing extra lines of code. I have seen few samples on this website that addresses my question but still i cannot put all the pieces together to solve what i am trying to achieve. Here is pseudo code of what i am trying to do: list<t> searchTerms; class t { string ...

LINQ casting with a Data.DataTableCollection

I have the following VB.NET code that I am using to sort a Data.DataTable by column count. For Each dtTarget As Data.DataTable In _ From x In Target.Tables _ Where DirectCast(x, Data.DataTable).Rows.Count > 0 _ Order By DirectCast(x, Data.DataTable).Columns.Count ... Next Is there a way to indicate that x is a Data.DataTab...

Linq to dataset select row based on max value of column

Hello, I have a dataset table, i want to group it by column "MOID", and then with in this group want to select the row which has max value of coulmn "radi" can anybody show me how to do it via LINQ to dataset ...

How to manually load an entity as well as a related entity

Hello Everyone, I have two entity objects one that holds billing address information(TBLADDRESS) and one that holds my account addresses(TBLMYACCOUNTADDRESS). At a point in my project i need to load the TBLADDRESS object with the corresponding values from TBLMYACCOUNTADDRESS. Both of which have a relation to LKSTATE. My problem is i c...

Selecting most recent news article in a collection with Linq to Sql

I have a bunch of news articles and I want to select the most recent one using Linq to Sql. I have an MVC action method like so [ChildActionOnly] public ActionResult LatestNews() { var article = mhndb.NewsArticles.Single(); return PartialView("LatestNews", article); } I would like to know the syntax for selecting the most rec...

LINQ to Entities Join on DateTime.DayOfWeek

Imagine two tables: Shifts, RANK_S_DAY. Shifts contains a ShiftDate column which is DateTime and RANK_S_DAY has a DayOfWeek column. I need to join (int)ShiftDate.DayOfWeek equals DayOfWeek. I understand why it won't work, but I'm not quite sure how I can change it. The Exception is: The specified type member 'DayOfWeek' is not su...

Need Help Translating SQL Server UNION Syntax to LINQ

Hi. I have the below SQL which works just fine: SELECT Message, CreateDate, AccountId, AlertTypeId FROM dbo.Alerts UNION SELECT TOP (100) PERCENT Status, CreateDate, AccountId, (SELECT 10 AS Expr1) AS AlertTypeId FROM dbo.StatusUpdates WHERE AccountId = PassedInParameter ORDER BY Cre...

How can I do SELECT UNIQUE with LINQ?

I have a list like this: Red Red Brown Yellow Green Green Brown Red Orange I am trying to do a SELECT UNIQUE with LINQ, i.e. I want Red Brown Yellow Green Orange var uniqueColors = from dbo in database.MainTable where dbo.Property == true select dbo.Color.Name; I then changed this to var uniqueColors = from dbo in database.Ma...

.NET Multi tier Design LINQ

Hi, I am quite new to the architecture and I am desiging an application for my next .NET project. My proposed architecture design is as follows: It is traditional three tier application which contains: DataLayer (LINQ + Partial Classes) BusinessLogicLayer (Entities + Validation Logic) (Optional) Service Layer (WCF) UI (Web site and Win...

Query by type using Linq on an nHibernate table by sub-class structure.

Hi, I have quite a complex entity structure where several classes inherit from a base class, hence choosing a table per-subclass structure in nhibernate. BaseProject ProjectA : BaseProject ProjectB : BaseProject ProjectC : BaseProject ProjectD : BaseProject I want to search where one of the criteria will be ProjectType. I'm trying to...

Call elements of an Linq executed stored procedure

Hi all, I created a Linq to SQL class in C#. Take the stored precedure in the dbml file. Added a new Datasource (Type Object). So i want to take the value of an Table that my Linq returns. LeasinggesellschaftDataContext blabla = new LeasinggesellschaftDataContext(); blabla.getDetails(2); MessageBox.Show(blabl...

Select entities where ID in int array - WCF Data Services, LINQ

I would like to return a set of entities who has and ID that is contained in a list or array of IDs using LINQ and Data Services. I know how to this using LinqToEF but I am at a loss how to this with Data Services or using OData query conventions for that matter. My thought is that I would do something like: int[] intArray = {321456, 3...

PLINQ update failed

Hi, sorry for my English. So, here is my question I'm trying to update DataTable by PLINQ Here is my code DataTable table = new DataTable(); table.Columns.Add(new DataColumn("val", typeof(decimal))); int N = 1000000; for (int i = 0; i < N; i++) table.Rows.Add(new object[] { i }); table.AsEnumerable().AsParallel().ForAll(row => row["...

Getting error while executing Ienumerable<Datarow>.Sum() method.

Why isn't the below code working? I am getting an error. It says 'Cannot implicitly convert type 'V' to int ' . private ObservableCollection<DataItem> method<T, V>(DataTable dt, SeriesItem seriesItem, FilterValues filter, ObservableCollection<DataItem> chart) { var result = from t in dt.AsEnumerable() ...

Simplifying and designing linq queries

In my new project, I`m writing a lot of linq. And I have a lot of linq queries like this: ... group new {A, B} by new {....} into g // Calculate select claw let SumVal1 = g.Sum(x => x.A.Value1 * func(x.A, x.B)) / g.Sum(x => func(x.A, x.B)) let SumVal2 = g.Sum(x => x.A.Value2 * func(x.A, x.B)) / g.Sum(x => func(x.A, x.B)) let SumVal3 = g...

LINQ - Include Child Property of Child Property

I have a child property of a child property in my entity. I have a Get method as such: List<T> Find(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] toInclude); The way I normally load just the first child property is to do this: myManager.Find(x => x.Id == id, x.ChildB); However, I would like to ...

XElements by Attribute Name

Hi All, I need to write a Linq query to get XElements based on Attribute Name. The XMl is not a structured one.. The attribute may there at the top of some XML node or any where in the xml ? Well actually it is the word document the xml file document.xml there will be places it uses the r:Id ,Now i need to get all the Elements which c...

Creating reusable Linq queries

I have a select query which is used over and over with different where filters: var query = from row in context.Table select row; How can I save this into a static class variable so I can reuse it in different methods? Like: var results1 = query.Where(condition1); ... var results2 = query.Where(condition2); ...