linq item get without foreach in selected new keyword

This question already has an answer here:

  • Linq code to select one item 8 answers

i wants to get a data using linq to SQL. I used for each like this is there is another way to get query data without for each .basically i wants to get from query without using for each loop because i know there is one object no need to used loop for it.

var query=from d in db.table where select new {}; foreach (var item in query) { string; }

-------------Problems Reply------------

Looks like you want only the first name, you don't need an anoymous type here, you can just use FirstOrDefault like this:

db.table.Where(d => == 2).Select(d =>;

This will return the name of the first record with id 2 or return null if there is no such record.

If you want all names use ToList instead of FirstOrDefault

You can use FirstOrDefault with your code like this (if is a string):

var query=(from d in db.table

Category:c# Views:2 Time:2017-09-10
Tags: linq

Related post

  • Select new keyword combination 2010-02-14

    In LINQ, what does the select new keyword combination do? I haven't found much documentation on this. Thanks --------------Solutions------------- By using select new, you can use the data or objects in the set you are working with to create new objec

  • Create a object in C# without the use of new Keyword? 2011-11-08

    Is there a way to create a object without the use of new keyword in C# some thing like class.forname(); in java. I want to dynamically create a object of a class. The object creation may depend on the users input. I have a base class x and there are

  • LINQ-to-SQL - 'Sum' inside a select new 2011-06-06

    I have a LINQ-to-SQL query that runs through a table, that I want to select 3 sum's - the sums of 'Rate' and 'AdditionalCharges', so I have something like this: var sums = from d in dc.Deliveries where d.TripDate == DateTime.Now select new { Rate = d

  • List items get mixed up after adding new items android 2014-06-26

    I have a ListView which displays a list of items fetched from the server. I have created a ALertDialog with EditText which takes user input and makes a API call and this user input gets added to the items server. Again the new updated items are displ

  • Accesing variable declared without a name (just new keyword) 2011-04-30

    I have a DataSet named dS in which i want to load XML. So i would use the function dS.ReadXml(); The paramter for ReadXml function is XmlTextReader Object. If i pass object as ds.ReadXml(new XmlTextReader(Application.StartupPath + "\\MyDataSource.xml

  • LINQ: Difference between 'Select c' and 'Select new (c-' 2009-08-10

    What is difference between these two statements: var result = from c in context.CustomerEntities join p in context.ProjectEntities on c.Pk equals p.CustomerPk where p.Entered > DateTime.Now.AddDays(-15) select c; and var result = from c in context

  • What's faster? select [object] or select new {[object].value1, [object].value2} with linq 2010-03-11

    I was hoping some of you guru's out there know the answer to this question. Which is faster? A or B A: var item = from d in database select d; B: var item = from d in database select new {d.item1, d.item2}; SEEMINGLY, it seems like selecting part of

  • Why use "select new " in LINQ 2011-09-04

    I am very new to LINQ to SQL, so please forgive me if its a layman sort of question. I see at many places that we use "select new" keyword in a query. For e.g. var orders = from o in db.Orders select new { o.OrderID, o.CustomerID, o.EmployeeID, o.Shi

  • Javascript Module Pattern and the new keyword 2012-04-18

    I get the basics of the Module Pattern and its use of a closure to allow for private members, BUT I can't quite swallow why the below code does what it does: var Calculator = function() { var priv = 0; return { changePriv: function() { priv++;}, prin

  • generic method to get linq result to datatable for select new with multiple selects 2011-07-18

    I'm trying to get the result of a join query with multiple selects into a dataTable, without selecting every fields separately. This is the setup: public class info { public int id {get;set;} public int linked { get; set; } public int someNumber { ge

  • LINQ - Is there a way to get element values after grouping without ForEach? 2011-10-07

    I have an Items table that contains a CategoryId field. This is an FK into a Categories table. I want to group my Items by CategoryId and return the Items and Categories.Name. I know I can do this in C#: var ItemsByCat = from i in Items group i by i.

  • how to add items to result of a linq select new command in C#? 2012-04-26

    in C# using linq i have a query like: var result = from p in cart select new { p.Product.Title, p.Product.Price }; now i want to add some another items to result. For example adding: int weight; string message; so that i can have something like: fore

  • Join Two Linq Queries into a Single Selection without foreach loop 2012-05-02

    I have a need to create a new model from two queries. I have a Member entity and a MemberTeam entity, with both of them having references to each other. What I need is a list of all members and to know if the member is assigned to a given team. I cur

  • LINQ query over a list of Bool+String pairs. How to join strings without foreach? 2011-12-20

    having some doubts with LINQ queries over objects... I have this filter descriptions which I want to keep private, and at given checkpoints in the code, I set some flags active. At the end of processing, I want to filter the active flags. If any flag

  • Does "Select New" in linq trigger an evaluation / load? 2009-05-01

    I'm currently trying to create a class which implements IEnumerable<T> in order to construct a Hierarchy from a flat list of objects which have references to each other through a ParentId property. I'd like to write a fluent interface for this

  • C# Silverlight - Getting Data Out Over The Wire Without Foreach & GetEnumerator() 2009-09-24

    Background Currently I have a C# Silverlight business application which uses RIA Services. The application is hosted in ASP.NET using the ADO.NET Entity Framework and a domain service class to read and write to the SQL Server database. Since the RIA

  • LINQ is select new necessary for efficiency 2011-01-29

    If I am selecting a subset of elements out of a list am I gaining anything by doing a select new to just get only the properties I will be using. Or does the compiler just optimize that out so I can stop encumbering myself with the select new, e.g.:

  • Linq - How can i use "this" inside of a "select new" statement 2011-08-12

    Edited: I'm querying some XML into objects recursively. Each object has a list of sub objects, and should refer to it's parent if it has one. Example XML: <object attribute1="text" attribute2="text"/> <object attribute1="text" attribute2="te

  • Get directly dictionary first key without foreach in C# 2012-03-29

    I have the following Dictionary content : [0] {75, SimpleObject} [1] {56, SimpleObject} [2] {65, SimpleObject} [3] {12, SimpleObject} ... I want to get first key, means 75, without foreach. Maybe the key word should reffers to [0] not 75. Because bas

  • Linq nested select new not working 2009-08-10

    I'm trying to get eager loading working with Subsonic, and it's been returning null for me. In the method below, I'm trying to hydrate a domain model (UserModel) which contains another domain model (CompanyModel). However, with the code below, UserMo

  • LINQ select new class with a name 2010-07-25

    I have a linq query where I am creating several classes which have a Parent property on them. I am looking for a way to set the parent property to be the class I just created. My explanation sucks; here's code of what I'm trying to do. var query = fr

  • Converting Foreach Loop to Linq and getting error 2010-10-28

    I've currently got the following foreach loop: List<SearchResult> searchResults = new List<SearchResult>(); foreach (Transmission trans in Results) { searchResults.Add(new SearchResult(trans)); } return searchResults; And I'd like to conv

  • LINQ select new error. Not able to access list 2011-02-21

    my Linq query is. string spouseName="jenny"; var empData = from sData in EmpList from member in sData.familyNames where string.Compare(member, spouseName, true) == 0 select new Employee { CompanyDept =, EmpName = sData.empName, FamilyNa

  • LINQ: get 'Use the new keyword to create an object instance' when more than one records are returned 2011-03-27 to LINQ and could do with some help. I'm querying some remote xml and using LINQ to put the results into a List. When the xml query only returns 1 record all is fine and i get a fully populated 'GetMPResult' object by accessing [0] item in the

Copyright (C), All Rights Reserved.

processed in 0.104 (s). 11 q(s)