Select Query In LINQ Based On Foreign Table

Mar 2, 2011

I have 2 Tables , OrderDetails and Requests In my LINQ to SQL dbml file. OrderDetailsID is a foreign key in Requests Table. I want to write the query to get the sum of UnitCost from OrderDetails based on OrderId. And If there is a row in Requests Table for each OrderDetailsID, and the Requests.Last.RequestType="Refund" I want to reduce the total refund amount from the main sum otherwise If there is no row based on OrderDetailsID, add to sum. Here is the way I implement that. I am looking to prevent using "For each".

iRefund = (From od1 In dc.OrderDetails _
Where od1.OrderID =1 _
Select od1.UnitCost).Sum
Dim objOrderDetails = (From od1 In dc.OrderDetails _
Where od1.OrderID =1 _
Select od1)
For Each OrderDetail As ORM.Entities.OrderDetail In objOrderDetails
If Not OrderDetail.Requests Is Nothing Then
IF OrderDetail.Requests.Last.RequestType="Refund" Then
iRefund -= OrderDetail.UnitCost
End If
End If
Next

View 1 Replies


Similar Messages:

Web Forms :: Select CheckBoxList Items Based On LINQ Query Results

Apr 29, 2010

[Code]....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace find.Admin
{
public partial
class WebForm1 : System.Web.UI.Page
{
private
class servicetimeofday
{
public int servicetimeofdayid {
get; set; }
public int serviceid {
get; set; }
public int timeofdayid {
get; set; }
}
protected void Page_Load(object sender,
EventArgs e
{
List<servicetimeofday> servicetimesofday =
new
List<servicetimeofday>
{
new servicetimeofday() { servicetimeofdayid = 1, serviceid = 1, timeofdayid = 1 },
new servicetimeofday() { servicetimeofdayid = 2, serviceid = 1, timeofdayid = 2 },
new servicetimeofday() { servicetimeofdayid = 3, serviceid = 2, timeofdayid = 1 },
new servicetimeofday() { servicetimeofdayid = 4, serviceid = 2, timeofdayid = 3 }
};
GridView2.DataSource = servicetimesofday;
GridView2.DataBind();
var itemstocheck = from
servicetimeofday in servicetimesofday
where servicetimeofday.serviceid == 2
select ne
servicetimeofday.timeofdayid
};
GridView3.DataSource = itemstocheck;
GridView3.DataBind();
foreach (var timeofdayid
in itemstocheck)
{
foreach (ListItem i
in CheckBoxList2.Items)
{
if (i.Value == timeofdayid.ToString())
{ i.Selected =
true; }
}
}
}
}
}

View 10 Replies

MVC :: Linq And C# Update Foreign Table

Jul 17, 2010

I have tables two tables, the "book" and the "author", a book can have many authors and vice versa, so there is an intermediate table called "book_author" with two columns as foreign key for those two primary keys

book_author
book_id
author_id

I'm trying to edit the book details and also its author using the entity date model, The controller class method

[Code]....

The HTML edit form

[Code]....

I can only update the book table, but not author that the book has or the book_author, I'm new to asp.net, I can't easily find a tutorial on this issue, I think the logic is to delete the author of the book first and then add the author for that book, but I don't know exactly about that linq and C# code. Can show me? I can only update the book table, but not author that the book has or the book_author, I'm new to asp.net, I can't easily find a tutorial on this issue, I think the logic is to delete the author of the book first and then add the author for that book, but I don't know exactly about that linq and C# code.

View 1 Replies

ADO.NET :: Call Field (foreign Key) On Another Table Using Linq?

Nov 12, 2010

how i call a image field from another table? the image field in the same table is working fine but the one on the other table is not. I have created the foreign key and made the connection... What else do i need to do to call that field? Page Load:

[Code]....

int COMPANYID = 0;

[Code]....

View 1 Replies

ADO.NET :: How To Insert Records In Multiple Table Using With Foreign Key With Linq To Sql

Feb 23, 2011

how to insert records in multiple table using with foreign key with linq to sql?

View 2 Replies

Query That Allow Duplicate Values In A Table But Not For Same Foreign Key Reference?

Mar 2, 2011

I am using SQL Server2005 with asp.net. I want validation at server side to restrict duplicate entries, Here i am using two tables companies and Branches. In Branches Table i had maintain a foreign key of CompanyId. In Branches the BranchName can be duplicate but not for the Particular CompanyId

View 2 Replies

ADO.NET :: Difference Between Select And Select New In Linq Query

Aug 17, 2010

What is difference between select { } and select new {} In Linq Query

View 3 Replies

Linq Query - How To Select Inner Query Into List

Oct 17, 2010

I am writing a linq query to select a blogpost,

[code]....

The blogpost i am testing got 3 tags attached to it. The table structure is:

(table)BlogPost -> (table)BlogPostTags <- (table)Tags

So the BlogPostTags table only contains 2 fields, BlogPostID and TagID.

When i run the query above i get 3 results back. Same blogpost 3 times but with 1 tag in each. It should return 1 post with 3 tags. The problem lies in the Tags query above.

View 1 Replies

DataSource Controls :: Select A Record From A Table And Insert Into B Table Using Linq?

Jun 29, 2010

how can i select a record from A table and insert into B table using linq?

View 2 Replies

LINQ Query Based On User Preferences?

May 12, 2010

I have a LINQ Query that includes an order by that is based on a user preference. The user can decide if they would like the results ordered asc or desc.

[code]....

View 2 Replies

C# - Delaying Linq To SQL Select Query Execution

Jun 14, 2010

I'm building an ASP.NET MVC site that uses LINQ to SQL. In my search method that has some required and some optional parameters, I want to build a LINQ query while testing for the existence of those optional parameters.

Here's what I'm currently thinking:
using(var db = new DBDataContext())
{
IQueryable<Listing> query = null;
//Handle required parameter
query = db.Listings.Where(l => l.Lat >= form.bounds.extent1.latitude && l.Lat <= form.bounds.extent2.latitude);
//Handle optional parameter
if (numStars != null)
query = query.Where(l => l.Stars == (int)numStars);
//Other parameters...
//Execute query (does this happen here?)
var result = query.ToList();
//Process query...

Will this implementation "bundle" the where clauses and then execute the bundled query? If not, how should I implement this feature?

View 1 Replies

ADO.NET :: LINQ If Statement Inside Select New Query?

Aug 18, 2010

My application includes BUILDING table with columns BUILDING_ID, NAME, TOWN_ID, I run the LINQ query listed below and need to append two letters to buildings.NAME based on TOWN_ID. I assume that I need to place an "if" or "select" statement inside the query but I can not figure out how to do that,

[Code]....

View 4 Replies

ADO.NET :: Select Corresponding Row In A Table Based On Selection In A Different Table

Jan 25, 2011

I have 3 tables (ShoppingCart, Data, Temporary) in database (Entities) I have no problems in getting data into ShoppingCart. I also know how to transfer data from columns in table ShoppingCart into columns of table Temporary (like EngName, CartID, Date, Quantity etc. - see code below) But I do not know how to multiply Quantity in table ShoppingCart by Price stored in table Data and save it into table Temporary. I think the problem is that I do not know, how to select coressponding row in table Data (see "var myCart2" in my code below) which holds the Price value for EngName previously selected by ("var myCart" also visible in the code). protected void Submit(object sender, EventArgse)

{
using (Entities
db = new Entities())
{
try
{
String
cartId = GetShoppingCartId();
var myCart = (from
cc in db.ShoppingCart
where cc.CartID ==
cartId select
cc);
foreach (ShoppingCart
item in
myCart)
{
int
i = 0;
if
(i < 1)
{
string
engname = item.EngName;
var myCart2 = (from
c in db.Data
where c.EngName ==
engname select
c);
Temporary ta =
new
Temporary();
Data d =
new
Data();

View 1 Replies

SQL Server :: Select Query Based On Two Fileds With Different Column

Dec 21, 2010

This is my Table structure:-Table1

Field1 Field2 Age

Dean Chen 20

Janel Wast 25

ann Miller 20


I used this select statement

select Field1+' - '+Filed2 from Table1

and i binded this in dropdownlist

now on selecting dropdownlist i need to show age in label. so how would be my select statement... to show age base on Filed1 and Filed2

View 6 Replies

ADO.NET :: Select Query For Linq On Default.aspx.cs Page?

Oct 9, 2010

how do i create linq query and asign that query to as data source to datalist on default.aspx.cs page

View 3 Replies

ADO.NET :: Linq Search - Dynamic Query Based On User Input?

Jan 12, 2011

I have two text boxes for the user to enter UserName & First Name, both these fields are optional and not mandatory to fill.

once the user presses the Search button, i execute the following query to fetch matching records.

[Code]....

The problem is, in cases where User leaves both the textbox or one of the textbox empty/blank, in that case that particular field should be removed from where clause.

More like if something is entered then search for that particular record otherwise search for all the records.

View 2 Replies

ADO.NET :: LINQ Query To Select Objects Containing Collection Of Keyword In A Property?

Dec 16, 2010

I'm new to linq. here is the scenario i need to write query for.I have a "Candidate" table with a varchar property "Skill"I have the table OR-mapped with LINQ to SQL on dbml file. Now I want to write linq query that:Selects all Candidates that have skill containing any of the keywords from ("asp.net","php","java")

View 1 Replies

Get The Value From SELECT Query From A Table?

Mar 23, 2011

how to get the value from SELECT query from a table

protected void Button_Click(object sender, EventArgs e)
{
string select_qry = "SELECT ID, USER, FILE, DATE, LASTUSED from FILE_INFO where USER = '" + user + "'"; // ID is primary key
SqlCommand cmd = new SqlCommand(select_qry);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = conn1;
conn1.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}

I have different USERS I want to get ID of user if user is different say 'Sheena' has ID value from 1-10 but if user is 'Sara' and she is having ID from 11-20 so I want to get specially ID of particular user how do I get ID from select query can any one know)

View 2 Replies

ADO.NET :: Linq To Sql Select Table At Runtime

Mar 15, 2011

I have several tables to load from depending on a string value using Linq to Sql I currently have

[Code]....

but of course that only gets the specific table DOffices if I wanted to grab tables at runtime what would be the best way to go about it?

View 1 Replies

ADO.NET :: Query Dynamic Table In LINQ?

Jan 17, 2011

I have a table created on the fly. The table structure is dynamic.

This is the table structure generated by the end of year 2010

DealerCode, 201010, 201011, 201012

This is the table structure generated by the end of Jan 2011

DealerCode, 201011, 201012, 201101

What I need to do is selecting all data and then bind it into a gridview

To build a LINQ query, we need to drag the table into dbml file first. As we don't know how the table look like, can we add it in dynamically? Are there any other solutions?

View 1 Replies

ADO.NET :: Dynamic Table Name Linq-to-sql Query?

Sep 20, 2010

I am writing a function in which i generate autocode in specific format i want to make this function generalize so that i send tablename and columnname parameter and function return the next code. I am using linq to sql with MVC e.g. i want to make table name and column name dyanamic

[Code]....

View 3 Replies

DataSource Controls :: Linq Query To Select Parent Records Which Have Related Children

Jan 19, 2010

I have a basic CMS which I'm adding to a site to display some downloads organised into groups. I have two tables, one for the groups and one for the downloads. I'm using a repeated to display the groups with another repeater inside to display the children. This works perfectly.

However some of my download groups may not have any downloads related to them and I'd like to handle this by filter the groups so that only those with a relate download record(s) are shown.

I'm trying to do this with the query which populates the top repeater based on some ideas I read but I must be going wrong with the syntax.

Here is what I'm using to try and only select downloads groups which have downloads linked to them by the download group ID.

[Code]....

Can anyone offer any thoughts on how I should construct the query to perform this?

View 1 Replies

ADO.NET :: Linq Query To Create HTML Table

Dec 26, 2010

I have a basic Linq query and what I need to do is query the DB then display the data in HTML.Typically I would bind this to a control but in this instance I need to actually generate the HTML in the code behind page.The Linq query is very basic something like this with Northwind:[Code]....

Instead of binding that to a gridview how can I write out the html... I'm sure I can use stringbuilder or smilar to create the html but I'm not sure how to get he data in the tables... foreach datarow in?

View 5 Replies

SQL Server :: Select Query On A Partitioned Table

Jul 20, 2010

I have partitioned a table and it has been partitioned according to an account id. ie also rows that have accountid =1 will be in A partition and all rows that have accountid = 2 will be in B partition. so now if i execute a query with where condition accountid =1 sql server will search only in A partition. Suppose if i add another condition to it ie where accountid=1 and place="aaa" will this query search only A partition or the entire table?

View 3 Replies

ADO.NET :: How To Wirte Linq Qurey To Select From And To Records In Table

Feb 9, 2011

can we wirte linq query to select few records based on the from and to records..

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved