linq-to-sql

Determine if record has children in LINQ to SQL

泪湿孤枕 提交于 2021-02-05 08:09:59
问题 I am having at hierarchical table with the structure ID, Name, FK_ID, Sortkey Fetching the data in LINQ to SQL is straight forward: var list = from ls in db.myTable where ls.FK_ID == levelId orderby ls.sortkey ascending select ls; And I can traverse down the tree by linking to the next levelId. But what I can't figure out, if there is a way in LINQ, to check if there is any children I could probably build a view, that added a flag to each record, but I would rather do this in LINQ, if

Determine if record has children in LINQ to SQL

天涯浪子 提交于 2021-02-05 08:09:12
问题 I am having at hierarchical table with the structure ID, Name, FK_ID, Sortkey Fetching the data in LINQ to SQL is straight forward: var list = from ls in db.myTable where ls.FK_ID == levelId orderby ls.sortkey ascending select ls; And I can traverse down the tree by linking to the next levelId. But what I can't figure out, if there is a way in LINQ, to check if there is any children I could probably build a view, that added a flag to each record, but I would rather do this in LINQ, if

Linq outer join using inequality?

走远了吗. 提交于 2021-02-05 05:48:10
问题 In SQL I'd say: select a.* from TableA a left join TableB b on a.Type = b.Type and a.SomeDate < b.AnotherDate where b.ID is null This would select all records in TableA where no record exists in TableB of the same Type and later date. In Linq, how do you do this? from a in TableA join b in TableB on a.Type equals b.Type into j // what about the comparator? from x in j.DefaultIfEmpty() where x == null select a; Thanks! EDIT: A few good answers have been proposed, all of which address the

Linq2SQL storing XElement to SQL Server database removes whitespaces

给你一囗甜甜゛ 提交于 2021-01-29 07:20:19
问题 Can I force System.Data.Linq.DataContext to store XML into a SQL Server table's XML column preserving whitespace or is there any other way? My test code is as follows: Guid MyNewQid = Guid.NewGuid(); using (DataClassesDataContext context = DataClassesDataContext.CreateDataContext()) { Guid myQID = Guid.Parse("{28da4eca-2c1a-4647-xxx-b398d1xxx013}"); FromSwiftBck t2sData = context.GetTable<FromSwiftBck>().FirstOrDefault(o => o.QID == myQID); string messageLoaded = t2sData.CompleteMessage; int

C# Linq Group by Object

帅比萌擦擦* 提交于 2021-01-28 09:11:09
问题 I have an issue of using group by in LINQ to SQL statement. The cod I have is var combinedItems = (from article in articles join author in authors on article.AuthorId equals author.Id into tempAuthors from tempAuthor in tempAuthors.DefaultIfEmpty() select new { article , author = tempAuthor}); var groups1 = (from combinedItem in combinedItems group combinedItem by combinedItem.article into g select g.Key).ToList(); var groups2 = (from combinedItem in combinedItems group combinedItem by

LINQ Sum of entries based on latest date

牧云@^-^@ 提交于 2021-01-28 04:57:19
问题 I have a table like so: Code | BuildDate | BuildQuantity --------------------------------- 1 | 2013-04-10 | 4 1 | 2014-09-23 | 1 1 | 2014-08-20 | 2 7 | 2014-02-05 | 4 I want the LINQ query to pick up the LATEST Build date for each Code, pick the BuildQuantity for that Code, and so on for all the records, and sum it up. So for the data above, the result should be 1 + 4 = 5 . This is what I'm trying: var built = (from tb in db.Builds orderby tb.BuildDate descending group tb by tb.Code into

The type 'X' is not mapped as a Table

烈酒焚心 提交于 2021-01-28 04:04:31
问题 I have ADO.NET Entity Data Model for my tables and if I use it directly it works fine. I wanted to write a wrapper, so that I don't have to repeat the same code again. Ended up writing some DataContext Extension methods. So, have something like this to get Data from the Table. public static TEntity Get<TEntity>(this DataContext dataContext, object id, string primaryKeyName) where TEntity : class, new() { if (id == null) return new TEntity(); var table = dataContext.GetTable<TEntity>(); return

Using Function logic in LINQ Query net core 3

橙三吉。 提交于 2021-01-27 20:08:00
问题 I have the following enum: public enum WorkType { Type1, Type2, Type3, Type4, Type5, Type6 } and a class public class Work { public WorkType Type {get; set;} .... } and an extension method: public static partial class WorkTypeExtensions { public static bool IsHighValueWork(this WorkType value) { switch (value) { case WorkType.Type1: case WorkType.Type2: return true; default: return false; } } } and SQL Linq query public List<Work> GetHighValueWork() { var query = Context.Work.Where( w => w

Linq To Sql - Get week day name from date

孤人 提交于 2021-01-27 18:43:40
问题 Is it possible to construct a query in which I can retrieve week day name from a date into a separate column or variable? I know I can very easily do this on .NET side but would like it to be in the query. 回答1: You can also use SqlFunctions... var results=context.Listings .Select(l=>System.Data.Entity.SqlServer.SqlFunctions.DateName("dw",l.modify_date)); Of course, this only works when using a SQL Server. Methods that are cross-database, would be to use EntityFunctions.DateDiff with a known

Return new LINQ object

一曲冷凌霜 提交于 2021-01-27 17:50:17
问题 I want to write LINQ which return me new object(string, int) contains: string (position name) int (count of position) Output: PositionA 8 PostionB 12 PostionC 13 Here is what I have so far: public List<string, int> TestL() //or IEnumerable? { var q1 = TestList.GroupBy(s => s.Postion.ToUpper()) .Select(d => { return new { NameDisplay = d.Key, Count = d.Count(s => s.PersonNR) }; }) .OrderBy(g => g.Key); return q1; } TestList have fields like: Postion, PersonNR, City, LastName. All the fields