示例程序参数(Example program argument)
示例程序:列出链接
此示例程序演示了如何从URL获取页面; 提取链接,图像和其他指针; 并检查他们的URL和文本。
指定要获取的URL作为程序的唯一参数。
package org.jsoup.examples; import org.jsoup.Jsoup; import org.jsoup.helper.Validate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class ListLinks { public static void main(String[] args) throws IOException { Validate.isTrue(args.length == 1, "usage: supply url to fetch"); String url = args[0]; print("Fetching %s...", url); Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); Elements media = doc.select("[src]"); Elements imports = doc.select("link[href]"); print("\nMedia: (%d)", media.size()); for (Element src : media) { if (src.tagName().equals("img")) print(" * %s: <%s> %sx%s (%s)", src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), trim(src.attr("alt"), 20)); else print(" * %s: <%s>", src.tagName(), src.attr("abs:src")); } print("\nImports: (%d)", imports.size()); for (Element link : imports) { print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel")); } print("\nLinks: (%d)", links.size()); for (Element link : links) { print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35)); } } private static void print(String msg, Object... args) { System.out.println(String.format(msg, args)); } private static String trim(String s, int width) { if (s.length() > width) return s.substring(0, width-1) + "."; else return s; } }
如何指定程序的唯一参数? 我是初学者所以不要打击我。 :)
Example program: list links
This example program demonstrates how to fetch a page from a URL; extract links, images, and other pointers; and examine their URLs and text.
Specify the URL to fetch as the program's sole argument.
package org.jsoup.examples; import org.jsoup.Jsoup; import org.jsoup.helper.Validate; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class ListLinks { public static void main(String[] args) throws IOException { Validate.isTrue(args.length == 1, "usage: supply url to fetch"); String url = args[0]; print("Fetching %s...", url); Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); Elements media = doc.select("[src]"); Elements imports = doc.select("link[href]"); print("\nMedia: (%d)", media.size()); for (Element src : media) { if (src.tagName().equals("img")) print(" * %s: <%s> %sx%s (%s)", src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"), trim(src.attr("alt"), 20)); else print(" * %s: <%s>", src.tagName(), src.attr("abs:src")); } print("\nImports: (%d)", imports.size()); for (Element link : imports) { print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel")); } print("\nLinks: (%d)", links.size()); for (Element link : links) { print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35)); } } private static void print(String msg, Object... args) { System.out.println(String.format(msg, args)); } private static String trim(String s, int width) { if (s.length() > width) return s.substring(0, width-1) + "."; else return s; } }
How do I specify the program's sole argument? I am a beginner so don't bash me. :)
原文:https://stackoverflow.com/questions/9280127
最满意答案
首先,你正在使用
SingleOrDefault
,它只会选择一个值。 你想要使用Sum
。 此外,您不需要匿名类型。 你可以使用:var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select (int) t2.Ammount).Sum();
或者,您可以使用
Sum
的形式进行投影,可能使用不同形式的Join
因为您只需要t2
值:var result = _dbEntities.Charges .Join(_dbEntities.ChargesTypes, t1 => t1.ChargesTypeID, t2 => t2.ID, (t1, t2) => t2) .Sum(t2 => (int) t2.Ammount);
或者甚至在连接中转换并使用“普通”
Sum
之后:var result = _dbEntities.Charges .Join(_dbEntities.ChargesTypes, t1 => t1.ChargesTypeID, t2 => t2.ID, (t1, t2) => (int) t2.Ammount) .Sum();
编辑:如果在
Ammount
列(它应该是Amount
)是一个NVARCHAR
然后a)如果可能可以更改它。 如果数据库类型合适,生活总是更简单; b)如果你不能:int.Parse
:var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select int.Parse(t2.Ammount)).Sum();
如果这不起作用,你可能需要在.NET端进行解析,例如
var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select t2.Ammount) .AsEnumerable() // Do the rest client-side .Sum(x => int.Parse(x));
Firstly, you're using
SingleOrDefault
which will only select a single value. You want to useSum
instead. Also, you don't need an anonymous type here. You can just use:var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select (int) t2.Ammount).Sum();
Alternatively, you could use the form of
Sum
which takes a projection, possibly using a different form ofJoin
as you only want thet2
value:var result = _dbEntities.Charges .Join(_dbEntities.ChargesTypes, t1 => t1.ChargesTypeID, t2 => t2.ID, (t1, t2) => t2) .Sum(t2 => (int) t2.Ammount);
Or even convert in the join and use the "plain"
Sum
afterwards:var result = _dbEntities.Charges .Join(_dbEntities.ChargesTypes, t1 => t1.ChargesTypeID, t2 => t2.ID, (t1, t2) => (int) t2.Ammount) .Sum();
EDIT: If the
Ammount
column (which should beAmount
by the way) in the database is anNVARCHAR
then a) change it if you possibly can. Life is always simpler if the database types are appropriate; b) useint.Parse
if you can't:var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select int.Parse(t2.Ammount)).Sum();
If that doesn't work, you may need to do the parsing on the .NET side, e.g.
var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select t2.Ammount) .AsEnumerable() // Do the rest client-side .Sum(x => int.Parse(x));
相关问答
更多-
如果你的子查询返回NULL ,这将导致尝试用NULL将数字求和,这会返回NULL 。 要解决这个问题,你可以尝试使用IFNULL函数,如果查询没有返回任何值,它将用0代替它。 SELECT k.id, s.fylke, s.Kommune, s.Skolenavn, k.schoolid, k.number, k.letter, SUM(e.amount) + IFNULL((SELECT SUM(poeng) FROM opp ...
-
SELECT a.AccountNumber, IFNULL( d.Amount, 0 ) - IFNULL( c.Amount, 0 ) AS Amount FROM 18_7_ChartOfAccounts AS a LEFT JOIN ( SELECT DebitAccount, SUM( Amount ) AS Amount FROM 2_1_journal GROUP BY DebitAccount ) d ON a.Ac ...
-
查询返回的总金额(Sum total amount returned from query)[2022-10-03]
您可以使用SUM聚合函数: select partno4pt,orders.orderdate,orders.processdate,orderdetails.qty,orderdetails.extprice, SUM(orderdetails.extprice) AS sumprice from orderdetails inner join orders on orderdetails.order_id = orders.order_id where orderdate ... -
也许一个简单的GROUP BY是你所要求的: SELECT to_char(p.date_from, 'Month') as month, sum(pl.amount) FROM hr_payslip p INNER JOIN hr_payslip_line pl ON (p.id = pl.slip_id AND pl.code = 'NET') GROUP BY to_char(p.date_from, 'Month'); I did it using the foll ...
-
我该如何总结这个查询(How can I sum on this query)[2023-01-02]
因为您正在使用错误column "users.id" must appear in the GROUP BY clause报告的postgres , column "users.id" must appear in the GROUP BY clause 。 您只需在请求结束时Group by users.id添加Group by users.id 。 Because you're using postgres as reported by the error column "users.id" must ... -
Mysql查询SUM动态金额(Mysql Query SUM dynamic amounts)[2022-03-20]
您需要使用聚合函数SUM()来获取每个帐户的总金额。 SELECT account, SUM(amount) totalAmount FROM tableName WHERE type = 'inventory' GROUP BY account MySQL GROUP BY(聚合)函数 You need to use an aggregate function SUM() to get the total amount for every account. SELECT account ... -
好的,你还没有回答我的问题,但是看看你希望在TXN_DATE过滤数据的sql查询。 我最好的猜测: DateTime dtFrom = DateTime.Today.AddDays(-3); DateTime dtTo = DateTime.Today; var query = from t in context.TRANSACTIONS .Where(tr=>tr.TXN_DATE>=dtFrom && tr.TXN_DATE<=dtTo) join s ...
-
总结与linq到sql的列(sum column with linq to sql)[2022-05-10]
假设金额列是双倍(可能是另一种类型) double sum = Table.Select(t => t.Amount ?? 0).Sum(); 要么 double sum = Table.Sum(t => t.Amount ?? 0).Sum(); 如果t.Amount为空,那么使用空coelescing操作符会给你一个默认值0。 Assuming the Amount column is a double (could be another type) double sum = Table.Selec ... -
首先,你正在使用SingleOrDefault ,它只会选择一个值。 你想要使用Sum 。 此外,您不需要匿名类型。 你可以使用: var result = (from t1 in _dbEntities.Charges join t2 in _dbEntities.ChargesTypes on t1.ChargesTypeID equals t2.ID where t1.StudentID == 1033 select ...
-
明年mssql总和金额(sum amount by next years mssql)[2022-02-15]
你想要的通常被称为跑步总数。 在SQL Server 2012中,您可以使用SUM() OVER ()来计算它。 SELECT workerID, Year, orderCount = SUM(orderCount), sumByYears = SUM(SUM(orderCount)) OVER (PARTITION BY workerID ORDER BY Year) FROM Orders GROUP BY workerID , Year ORDER BY workerID , Ye ...