首页 \ 问答 \ 示例程序参数(Example program argument)

示例程序参数(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
更新时间:2023-12-19 11:12

最满意答案

首先,你正在使用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 use Sum 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 of Join as you only want the t2 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 be Amount by the way) in the database is an NVARCHAR then a) change it if you possibly can. Life is always simpler if the database types are appropriate; b) use int.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));

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。